【发布时间】:2023-04-03 20:37:02
【问题描述】:
我正在尝试发布以下请求,但收到“不支持的媒体类型”响应。我将 Content-Type 设置为 application/json。任何帮助,将不胜感激。根据下面的评论,如果我将内容更改为 'new StringContent(JsonConvert.SerializeObject(root), Encoding.UTF8, "application/json")' 那么我会收到错误的请求响应
string URL = "https://test.com/api/v2/orders/"; //please note it is dummy api endpoint
var client = new HttpClient();
var httpRequestMessage = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(URL),
Headers = {
{ HttpRequestHeader.Authorization.ToString(), "Bearer ABcdwenlfbl8HY0aGO9Z2NacFj1234" }, //please note it is dummy bearer token
{ HttpRequestHeader.Accept.ToString(), "application/json;indent=2" },
{ HttpRequestHeader.ContentType.ToString(), "application/json" }
},
//Content =new StringContent(JsonConvert.SerializeObject(root), Encoding.UTF8, "application/json")
Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(root))
};
var response = client.SendAsync(httpRequestMessage).Result;
【问题讨论】: