【发布时间】:2019-05-18 03:02:09
【问题描述】:
问题
我想POST JSON 请求使用 Flurl 并指定内容标头 Content-Language。我已经成功地设置了内容类型 (Content-Type),没有任何问题:
string response = await "https://jsonplaceholder.typicode.com/".AppendPathSegment("posts")
.WithHeaders(new { Content_Type = "application/json; charset=UTF-8" })
.PostJsonAsync(new { title = "bar", body = "foo", userId = 1 }).ReceiveString();
Console.WriteLine(response);
正确返回:
{
"title": "bar",
"body": "foo",
"userId": 1,
"id": 102
}
但是,尝试另外指定Content-Language:
string response = await "https://jsonplaceholder.typicode.com/".AppendPathSegment("posts")
.WithHeaders(new { Content_Type = "application/json; charset=UTF-8", Content_Language = "en-US" })
.PostJsonAsync(new { title = "bar", body = "foo", userId = 1 }).ReceiveString();
Console.WriteLine(response);
打破给出错误的代码
错误的标题名称。确保请求标头与 HttpRequestMessage 一起使用,响应标头与 HttpResponseMessage 一起使用,内容标头与 HttpContent 对象一起使用。
我知道应该在内容而不是请求上设置内容标头,但是,在阅读此answer(和this one)之后,似乎不再需要使用 Flurl 2.0+ 区分请求和内容级别.
旁注
使用的 API 是一个模拟。我知道使用PostJsonAsync 会自动设置Content-Type 标头,但是,对于我的用例,它将设置为application/vnd.api+json,因此需要明确指定。
问题
我做错了什么?关于内容标题,我有什么不明白的地方吗?
将Content-Language 添加到请求中时,Content-Type 与Content-Type 有何不同?
【问题讨论】:
标签: c# header http-headers flurl