Samantha J T Star
Samantha J T Star

Reputation: 32808

How can I find out the value of the keys in a dictionary object?

I have this API call:

HttpResponse<string> response = 
    Unirest.get("https://wordsapiv1.p.mashape.com/words/" + word.Name)
    .header("X-Mashape-Key", "xxxx")
    .header("Accept", "application/json")
    .asJson<string>();

Here is the class for the HttpResponse:

public class HttpResponse<T>
{
    public HttpResponse(HttpResponseMessage response);

    public T Body { get; set; }
    public int Code { get; }
    public Dictionary<string, string> Headers { get; }
    public Stream Raw { get; }
}

I have no problem getting the Body (response.Body) or the Code but what I would like to do is to get this header:

[7] = {[X-RateLimit-requests-Remaining, 2498]}

Can someone tell me how I could check the response returned and find out the value of the X-RateLimit-requests-Remaining?

Upvotes: 1

Views: 131

Answers (1)

Manfred Radlwimmer
Manfred Radlwimmer

Reputation: 13394

Dictionaries have something called an indexer. The datatype of your indexer is the datatype of your Key (Dictionary<Key,Value>).

Indexers are similar to property getters and setters and are implemented like this:

public TValue this[TKey index]
{
    // this will return when being called e.g. 'var x = dictionary[key];' 
    get { return whatever; }

    // and here 'value' is whatever you pass to the setter e.g. 'dictionary[kex] = x;'
    set { whatever = value; }
}

In you case that would be:

// "Key" is just an example, use "X-RateLimit-requests-Remaining" instead ;)
response.Headers["Key"]; 

Upvotes: 4

Related Questions