rest - Dio options.contentType 与标题“Content-Type”
问题描述
我试图使用Dio 插件调用 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
手动设置标题?
解决方案
我已经在本地尝试过这个dio: ^3.0.10
,它似乎ContentType.json
是一个无效的值contentType
。
应该使用挖掘dio 的文档。Headers.jsonContentType
推荐阅读
- python - 有没有办法解决这个 smtp 连接错误?
- bash - 在 bash 中从键盘读取 n 个值
- java - 在 Java 中模拟链表
- reactjs - 对预检请求的响应未通过访问控制检查:它没有 HTTP ok 状态 - React 问题
- python - 找出任何两个集合是否具有相同的成员资格
- git - git push 不删除已删除的文件
- python - 使用 if else 语句编写列表理解
- ios - 有谁知道如何修复线程 1:信号 SIGABRT 错误?我已经尝试过清洁我的产品。我是 iOS 开发新手
- java - Logback 创建日志文件但不向其中写入任何内容
- java - 循环内的 ArrayList