【发布时间】: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 标头?
【问题讨论】:
-
在它声明的文档中(对于
contentType)The request Content-Type. The default value is [ContentType.json]。所以也许你应该省略它?