rest - Dio options.contentType 与 header "Content-Type"

标签 rest http flutter dart

我正在尝试使用 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 header :

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

所以现在我很困惑 - contentType 选项到底做了什么?我认为这类似于手动设置 Content-Type header ?

最佳答案

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

invalid contentType

挖掘 documentation for dio , 应使用 Headers.jsonContentType

valid contentType

关于rest - Dio options.contentType 与 header "Content-Type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57586714/

相关文章:

ios - 构建成功后,应用程序启动没有返回有效的对,也没有在 Xcode 上返回启动错误

rest - Delphi XE3 - datasnap REST 服务器 UTF8 输出

python - 安全存储 REST API 凭据以便在 Python 环境中进行访问

python - 为什么 Python 无法请求解析 TikTok API 数据?

.htaccess - 没有遵循重定向

flutter - 有没有办法禁用 PageView 裁剪效果?

javascript - 如何在 Node.js 中通用化 http 响应处理程序?

php - 在 PHP 中使用 REST API 在 SalesForce 中创建销售线索

apache 服务器 - 如何将 http 重定向到 https

authentication - 在 Flutter 中使用 Google Sign-In 获取用户的生日/性别