我正在尝试使用 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.json
是 contentType
的无效值.
挖掘 documentation for dio , 应使用 Headers.jsonContentType
。
关于rest - Dio options.contentType 与 header "Content-Type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57586714/