-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
System.InvalidOperationException: Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects. #33039
Comments
@davidsh any suggestion please |
Entity body related headers such as 'Content-Type', 'Content-Length' should be added to the Content.Headers collection of HttpRequestMessage. That assumes that you are also adding an HttpContent object as well since you are trying to probably post some content. request.Content.Headers.Add("Content-Type", "application/json;charset=UTF-8"); |
Thank you for your repsonse
Cordailly |
|
@davidsh Can you invetigate please |
System.NullReferenceException: Object reference not set to an instance of an object, when i used request.Content.Headers.Remove("Content-Type");
|
You need to set Content to something. The null ref is from your method trying to call something off of the Content property, which will return null until you set it to something non-null. |
Why do you want to set the 'Content-Type' header at all? If you are trying to control the HTTP response body encoding, then you should set the 'Accept-Encoding' header instead. |
thnaks for all response but still same error,
|
Where are you setting request.Content? |
request.Content.Headers.Add("Content-Type", "application/json;charset=UTF-8"); |
No, where are you actually setting the Content property, e.g.
? If you don't do that, it will return null. |
https://docs.microsoft.com/fr-fr/aspnet/web-api/overview/advanced/http-message-handlers |
My code work well using .NET Framework |
You must create a .Content object if you plan on adding content related headers to the request object like 'Content-Type'. Also, you didn't answer my question here about why you want to use 'Content-Type' header at all. |
i used Content_Type to specifies to the server what data it should expect |
Ok. That implies you are sending data to the server? So, where is the data that you're sending? You usually create an |
No in this case i need to get Data from server |
|
|
If you need to set 'Content-Type', you should set it on your request.Content.Headers object. However, looking at your code, you probably already are setting it? request.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
This statement tells me you want to control the data that the server sends to the client. So, you should be setting the 'Accept-Encoding' header on the HttpRequestMessage instead. That tells a server what kind of response body data to send back to the client. |
|
system.Exception: Exception of type 'System.Exception' was thrown. |
Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.
|
If you want to use 'Accept-Encoding', that header goes into the main request.Headers collection. |
system.Exception: Exception of type 'System.Exception' was thrown. |
Can you came skype please for 2 minutes please |
i have modified my code like this
|
|
my request return this messge below |
I have finally find solution, i don't need to use request.Headers.Add("Accept-Encoding", "gzip");
Thanks for all response @davidsh @stephentoub |
Hello,
I have migarted my project from .NET Framework to .NET Core, and when i call WS i get error
System.InvalidOperationException: Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.
at System.Net.Http.Headers.HttpHeaders.GetHeaderDescriptor(String name)
at System.Net.Http.Headers.HttpHeaders.Add(String name, String value)
The text was updated successfully, but these errors were encountered: