HttpClient / .NET Core不支持的媒体类型 [英] Unsupported Media Type with HttpClient / .NET Core
问题描述
我正在使用支持JSON格式的POST请求的RESTful API。 API自己的Swagger文档显示这是对其端点之一的有效调用:
I am working with a RESTful API that supports POST requests in JSON format. The API's own Swagger documentation shows that this is a valid call to one of its endpoints:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '<JSON>' '<URL>'
其中< JSON>
和< URL>
是有效的JSON消息和端点的URL。从Swagger文档中,我收集到该终结点的任何帖子都必须同时包含 Content-Type
和 Accept
标头 application / json
。
Where <JSON>
and <URL>
are the valid JSON message and the endpoint's URL. From this Swagger documentation I gather that any posts to this endpoint must include both a Content-Type
and Accept
headers set to application/json
.
我正在编写C.方法,该方法将使用.NET Core的 HttpClient
类发布到此端点。但是,发布消息后,我收到HTTP 415错误代码,表示不支持的媒体类型。根据到目前为止的了解,必须在您的内容中设置 Content-Type
标头(我正在使用 StringContent
类)和 Accept
标头只能在 HttpClient
标头中设置。这是我的特定示例:
I am writing a C# method that will use .NET Core's HttpClient
class to post to this endpoint. However, upon posting my message I receive an HTTP 415 error code back, for Unsupported Media Type. From what I've learned so far, the Content-Type
header must be set in your content (I am using the StringContent
class) and the Accept
header can only be set in the HttpClient
headers. Here is my particular example:
var httpContent = new StringContent("<JSON>", Encoding.UTF32, "application/json");
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var responseMessage = httpClient.PostAsync("<URL>", httpContent);
var result = responseMessage.Result;
再次,其中< JSON>
和< URL>
是有效的JSON消息和端点的URL。在我看来,我引用 httpCllient.DefaultRequestHeaders
的第三行没有添加 Accept:application / json
我请求的标头。如果我将标头手动添加到 httpContent.Headers
集合中,则会收到运行时错误,告诉我 Accept
不是可以添加到 httpContent
的标头。这就是为什么我希望将其添加到 httpClient
的原因。
Once again, where <JSON>
and <URL>
are the valid JSON message and the endpoints's URL. It would seem to me that the third line, on which I reference httpCllient.DefaultRequestHeaders
, is not adding the Accept: application/json
header to my request. If I manually add the header to the httpContent.Headers
collection, I get a run-time error that tells me that Accept
is not a header I can add to the httpContent
. That's why I am hoping to add it to the httpClient
instead.
我已经使用Swagger验证了URL和JSON,所以我知道它们是正确的。另外,请求是通过HTTPS完成的,因此我无法使用Fiddler来验证是否包含 Accept
标头。虽然我可以在Fiddler中启用解密,但这是另外一个蜡球。我不想将其根证书添加到我的系统中,特别是如果我缺少一些看起来很简单的东西时。
I have validated the URL and my JSON with Swagger, so I know those are correct. Also, the request is done over HTTPS, so I can't use Fiddler to validate that the Accept
header is being included. And while I could enable decryption in Fiddler, that's a whole other ball of wax. I don't want to add their root certificate to my system, especially if I'm missing something fairly simple, which this seems to be.
任何指针将不胜感激。谢谢。
Any pointers will be appreciated. Thanks.
推荐答案
如果您尝试以下操作:
var httpContent = new StringContent(jsonData, Encoding.UTF8, "application/json");
您不需要添加Accept标头。
You shouldn't need to add an Accept header.
这篇关于HttpClient / .NET Core不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!