首页 > 解决方案 > 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手动设置标题?

标签: resthttpflutterdart

解决方案


我已经在本地尝试过这个dio: ^3.0.10,它似乎ContentType.json是一个无效的值contentType

无效的内容类型

应该使用挖掘dio 的文档Headers.jsonContentType

有效的内容类型


推荐阅读