1

i am using HttpClient to send request, i want to use my custom request headers using HttpClient in GET Method?

Here is my code:

public HttpResponseMessage Get(string url, List<KeyValuePair<string, string>> headers = null)
    {
        HttpRequestMessage request = new HttpRequestMessage()
        {
            RequestUri = new Uri(url),
            Method = HttpMethod.Get,
        };
        if (headers != null && headers.Count > 0)
        {
            foreach (var header in headers)
            {                    
                request.Headers.Add(header.Key, header.Value);

            }
        }            
        HttpResponseMessage response = httpClient.SendAsync(request).Result;
        return response;
    }

But it threw an error at request.Headers.Add(header.Key, header.Value);

Below is the error message:

Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

Any help would be appreciated

Lee Liu
  • 1,981
  • 1
  • 12
  • 13
  • Did you debug the code? What values you have in `header.Key` and `header.Value` when you get the exception? – Chetan Jan 24 '19 at 09:20
  • My header is "Content-Type" "application/json". I found the workaround at https://stackoverflow.com/questions/10679214/how-do-you-set-the-content-type-header-for-an-httpclient-request, but it is only suitable for Http Post method – Lee Liu Jan 24 '19 at 09:23
  • For GET request setting Content-Type header is not valid. You simply can not set Content-Type header for GET requests. You should not. – Chetan Jan 24 '19 at 09:28
  • @LeeLiu that's not a workaround, that's the answer. GET has no content so using `Content-Type` is a bug. If you want to request a specific content type use the `Accept` header – Panagiotis Kanavos Jan 24 '19 at 09:29
  • @ChetanRanpariya If i want to set my customer header like "domain:005", how can i do that? – Lee Liu Jan 24 '19 at 09:30
  • @PanagiotisKanavos if i want to use my custom request headers like "domain" "005", how can i achieve it? – Lee Liu Jan 24 '19 at 09:31
  • @LeeLiu you already do it. The problem is that you used an invalid header – Panagiotis Kanavos Jan 24 '19 at 09:33
  • @PanagiotisKanavos as you said above, it is not a workaround, why do you set my this thread to be duplicate – Lee Liu Jan 24 '19 at 09:33
  • Then you should not have Content-Type in the `headers` collection? Or you can check for the header name in foreach loop and do not add it to request. – Chetan Jan 24 '19 at 09:34
  • @LeeLiu because it is. You don't need a workaround, you tried to use an invalid header name and the answers to the duplicates explain this. They show how to set request and content headers using the correct objects – Panagiotis Kanavos Jan 24 '19 at 09:34
  • @PanagiotisKanavos I need to use customer headers via httpclient,i can do that with Post method, but failed with Get Method. – Lee Liu Jan 24 '19 at 09:34
  • @LeeLiu you already wrote that multiple times. Repeating it won't change the fact that a) you *can*, with the very code you used and b) you can't use *invalid* names. Post a *specific example* - which header did you try to set, using which value and which error did you get. Please don't say `content-type`, as everyone in the comments explained, that's *invalid* for GET requests – Panagiotis Kanavos Jan 24 '19 at 09:36
  • @LeeLiu the second duplicate shows how to set headers. The code isn't really different from you code but it shows how to set the `Accept` header for a GET request – Panagiotis Kanavos Jan 24 '19 at 09:38
  • @PanagiotisKanavos Thank you for your patience, i have find the solution, we can use "request.Headers.TryAddWithoutValidation(header.Key, header.Value);" to achieve it. – Lee Liu Jan 24 '19 at 09:48
  • @LeeLiu that's no solution, that's covering up the problem. Why do you insist on using an *invalid* header? – Panagiotis Kanavos Jan 24 '19 at 09:49
  • @PanagiotisKanavos Beacuse i have the requirement that it needs to set arguments in request headers, for example: "user_Token : xxxxxxx", not only the "Content-Type" – Lee Liu Jan 24 '19 at 09:52
  • @PanagiotisKanavos I used WebClient before, it has no problem, but with HttpClient, it failed. it's so terrible. – Lee Liu Jan 24 '19 at 09:53
  • @LeeLiu then you can use the code you already posted. Post an example that demonstrates your actual problem. You should be able to use `user_Token`. You aren't supposed to use `content-type` – Panagiotis Kanavos Jan 24 '19 at 09:53
  • @LeeLiu that's why WebClient is obsolete for the past 7 years, it allowed a lot of buggy calls. – Panagiotis Kanavos Jan 24 '19 at 09:54
  • @PanagiotisKanavos it's my fault that didn't explain clearly,thank you again – Lee Liu Jan 24 '19 at 09:58

1 Answers1

2

You should do this with using your HttpClient instance like below:

httpClient.DefaultRequestHeaders
      .Accept
      .Add(new MediaTypeWithQualityHeaderValue("x-your-custom-header"));
Saman Gholami
  • 3,416
  • 7
  • 30
  • 71