【问题标题】:Specifying content language for JSON request with Flurl?使用 Flurl 为 JSON 请求指定内容语言?
【发布时间】: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-TypeContent-Type 有何不同?

【问题讨论】:

    标签: c# header http-headers flurl


    【解决方案1】:

    对于遇到相同问题的每个人来说:这是一个错误。我提交了issue on GitHub 并且维护者批准了它,说它将在下一个版本中修复(据说是Flurl.Http 2.4.1)。
    除了Content-Language,标头AllowContent-Encoding 也丢失了,并且会抛出与问题中描述的相同的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-14
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2018-03-10
      相关资源
      最近更新 更多