我想使用 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
头部信息?