HttpClient/.NET Core 不支持的媒体类型 [英] Unsupported Media Type with HttpClient / .NET Core

查看:26
本文介绍了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.从这个 Swagger 文档中,我收集到此端点的任何帖子都必须包含设置为 application/jsonContent-TypeAccept 标头.

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.在我看来,我引用 httpClient.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆