0

I am trying to make a GET request in C# and need to set the Content-Type header to application/json and set a header with my api key. I have tried searching but surprisingly have not found a straightforward way to do this.

This answer is very close to what I want, because they are able to set the Content-Type header here, but I also need to set a custom header for my api key and StringContent does not have additional fields for such a thing.

request.Content = new StringContent("{\"name\":\"John Doe\",\"age\":33}",
                                    Encoding.UTF8, 
                                    "application/json");//CONTENT-TYPE header

^ their answer, for reference. This is my code, but I get a null result. I know that I can hit the api in postman with these values, but I am assuming the way I configure my HttpRequestMessage is wrong so the request fails.

HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage
 {
   Method = HttpMethod.Get        
 };

// need Content to be not null, this seems wrong   
request.Content = new StringContent("");

// need to set Content-Type to application/json
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
// set api-key header
request.Content.Headers.Add("api-key", apiAdminKey);

request.RequestUri = new Uri($"{URL}?id={id}");

return client.SendAsync(request, HttpCompletionOption.ResponseContentRead);

Any advice?

Update

Got it working with just HttpClient, for some reason I thought I could not set the headers using client.DefaultRequestHeaders.Add.

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("api-key", apiAdminKey);
return client.GetAsync($"{URL}?id={id}");

Thanks for the help!!!!

Kah
  • 627
  • 2
  • 11
  • 28
  • 1
    Use [Fiddler](https://www.telerik.com/fiddler) and compare your request with Postman – Eser May 04 '18 at 19:44
  • HttpRequestMessage.Headers.Add("id","somevalue")..this has generally worked for me. Could you provide a sample request and the expected request for comparison? – Chandra Sekhar V May 04 '18 at 19:50
  • I would suggest you use `WebRequest` parsed with `HttpWebRequest` which is really great in your case. If you don't mind changing the whole code, let me know and I will post full reply with it. – Ahmed Õşşama May 04 '18 at 19:51
  • @ChandraSekharV it says that the Headers property cannot be assigned to, it is read only – Kah May 04 '18 at 19:55
  • @Eser using Fiddler I see the Request Headers have no content-type header or my api key header set. I am not surprised, and want to know how to set these values – Kah May 04 '18 at 20:02
  • @AhmedÕşşama I will try that - thanks. I assumed HttpClient/HttpRequestMessage would have support for custom headers – Kah May 04 '18 at 20:03
  • @AhmedÕşşama `I would suggest you use WebRequest` Why to use old fashion methods instead of more modern way of HttpClient? @Kelsey see also `httpClient.DefaultRequestHeaders.TryAddWithoutValidation` There is nothing you can not handle with HttpClient. Just read some docs – Eser May 04 '18 at 20:23
  • 1
    You are adding api-key as content header but it should be request header and not content header unless your API is implemented that way. Use `httpClient.DefaultRequestHeaders` to add api key. – Pankaj Kapare May 04 '18 at 20:25

0 Answers0