1

I am using HttpClient for rest service . At at one point I have a problem when I try to add "Content-Type" in my 'Get' request header .

I know "Content-Type" is suitable for content send in request body part But It's my need i have to send "Content-Type" with request header part.

I also try to remove "Content-Type" header from Invalid Header list of HttpRequestHeaders

I find link How do you set the Content-Type header for an HttpClient request?

        Dim field = GetType(System.Net.Http.Headers.HttpRequestHeaders).GetField("invalidHeaders", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.[Static])

        If field IsNot Nothing Then
            Dim invalidFields = DirectCast(field.GetValue(Nothing), HashSet(Of String))
            invalidFields.Remove("Content-Type")
        End If

But my issue not resolved I have exception

InnerException:

Message=The 'content-type' header must be modified using the appropriate property or method.

   StackTrace:
        at System.Net.WebHeaderCollection.ThrowOnRestrictedHeader(String headerName)
        at System.Net.WebHeaderCollection.Add(String name, String value)
        at System.Net.Http.HttpClientHandler.SetRequestHeaders(HttpWebRequest webRequest, HttpRequestMessage request)
        at System.Net.Http.HttpClientHandler.CreateAndPrepareWebRequest(HttpRequestMessage request)
        at System.Net.Http.HttpClientHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   InnerException:

I find some tools like Postman or SoupUI allow this .

Please help me to find some solution .

Thanks

Community
  • 1
  • 1
Shubham
  • 183
  • 1
  • 3
  • 15
  • So what is your requirement ? you want to add Content-Type header in your request or you want to remove it? Are you facing any issues while adding it? – Chetan Jan 25 '17 at 07:00
  • http://stackoverflow.com/questions/29801195/adding-headers-when-using-httpclient-getasync Follow this post to understand how to add header to request when using httpClient to call GET API. – Chetan Jan 25 '17 at 07:10
  • Its usefull link but actual problem is i want to add content-type in header like `client.DefaultRequestHeaders.Add("Content-Type","apllication/json")` – Shubham Jan 25 '17 at 07:49
  • It allows me to add the Accept header but when I try to add Content-Type it throws the following exception: Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects. – Shubham Jan 25 '17 at 07:57
  • It seems the HTTP rule which does not allow setting "Content-Type" for GET requests. http://stackoverflow.com/questions/26109650/explicitly-set-content-type-headers-for-get-operation-in-httpclient Can you explain your requirement which forces you to add this header to the request? – Chetan Jan 25 '17 at 08:41
  • APIs (such as the **LiquidFiles Api**) requires setting the Content-Type header for a GET request but in .net not allow to setting this header and – Shubham Jan 25 '17 at 09:20
  • Hmm... Makes sense.. Found another source which provides solution to this using reflection http://stackoverflow.com/questions/10679214/how-do-you-set-the-content-type-header-for-an-httpclient-request/16959464 – Chetan Jan 25 '17 at 10:01
  • But using reflection is work when I used **System.Net.Http (2.0.0.0)** on **.Net 4** but not working on updated supported version **System.Net.Http (2.2.29.0)** for **.Net 4** – Shubham Jan 25 '17 at 10:38
  • I have posted answer of using HttpWebRequest instead of HttpClient. I will serve your purpose of calling API with GET with Content-Type header set. Please have a look. – Chetan Jan 25 '17 at 10:42

1 Answers1

0

HttpWebRequest may come to your rescue.

private static string CallService(string url)
{
    WebRequest req = WebRequest.Create(url);
    req.Method = "GET";
    String json;
    req.ContentType = "application/json; charset=utf-8";
    var resp = req.GetResponse();
    using (varstream = resp.GetResponseStream())
    {
        var re = new StreamReader(stream);

            json = re.ReadToEnd();
        }

        return json; 
    }
}

Async implementation of this can be found from Getting the Response of a Asynchronous HttpWebRequest

Community
  • 1
  • 1
Chetan
  • 6,711
  • 3
  • 22
  • 32
  • It's useful But some reason like benefit of HttpClient over WebRequest . so i choose httpclient for implementation. I hope you understand so please give me another solution . – Shubham Jan 25 '17 at 12:18
  • HttpClient does not support this feature by itself unless you wish to hack it. One such hack is explained here http://stackoverflow.com/questions/10679214/how-do-you-set-the-content-type-header-for-an-httpclient-request/16959464 – Chetan Jan 25 '17 at 12:37
  • I tried But using reflection is work when I used System.Net.Http (2.0.0.0) on .Net 4 but not working on updated supported version System.Net.Http (2.2.29.0) for .Net 4 – Shubham Jan 27 '17 at 05:13
  • Thanks Chetan for all the answers to my queries , if you have more information on the same please share with me in future . – Shubham Jan 27 '17 at 05:17