【问题标题】:Dio options.contentType vs header "Content-Type"Dio options.contentType 与标题“Content-Type”
【发布时间】:2019-12-26 10:37:29
【问题描述】:

我尝试使用 the Dio plugin 调用 REST 服务,但一直收到 HTTP 400 响应代码。我认为通过将内容类型和响应类型选项设置为 JSON,我做的一切都是正确的:

Response response = await Dio().get(
    'https://api.example.com/v1/products/$productId',
    queryParameters: {},
    options: Options(
        contentType: ContentType.json,
        responseType: ResponseType.json,
        headers: {'Authorization': 'Bearer $MY_API_KEY'}
    ),
);

然而,事实证明我还需要添加一个Content-Type 标头:

headers: {'Authorization': 'Bearer $MY_API_KEY'}, 'Content-Type': 'application/json' };

所以现在我很困惑 - contentType 选项到底有什么作用?我以为这类似于手动设置Content-Type 标头?

【问题讨论】:

  • 在它声明的文档中(对于contentTypeThe request Content-Type. The default value is [ContentType.json]。所以也许你应该省略它?

标签: rest http flutter dart


【解决方案1】:

我已经在本地使用 dio: ^3.0.10 进行了尝试,似乎 ContentType.jsoncontentType 的无效值。

应该使用documentation for dioHeaders.jsonContentType

【讨论】:

    猜你喜欢
    • 2014-03-06
    • 2019-11-13
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 2021-11-08
    • 1970-01-01
    相关资源
    最近更新 更多