美文网首页
flutter Dio设置Content-Type的问题

flutter Dio设置Content-Type的问题

作者: DasonXie | 来源:发表于2020-11-04 16:17 被阅读0次
  • 问题

后台获取的content-type不是在请求是设置的content-type。

这个问题是有下面的set方法引发的(在dio的options.dart文件中)
  • 解决方案

描述太长,先看结论:这个问题在不同的设置顺序中会有不同的结果,但是在初始化之后,再对options设置content-type就可以达到我们想要的效果

//在options初始化后,再设置contentType!!不直接设置header。
 options.contentType = 'text/plain';
  • 问题重现

情况一:在初始化时设置options的content-type

1、content-type="text/plain"通过初始化baseOptions方法设置header的content-type
2、content-type=null初始化后半段会初始化contentType属性,这个位置会重新设置header的content-type。
3、content-type="application/json"实际request请求的时候,会整合dio对象中的baseOptions和request传的options,那么又会出现设置 contentType 的情况,但是在这里会有默认的contentType 为application/json
****所以在初始化的时候设置了content-type="text/plain"但是请求的时候content-type="application/json"****

情况二:在初始化时设置options的Content-Type首字母大写

1、Content-Type="text/plain"通过初始化baseOptions方法设置header的Content-Type
2、Content-Type="text/plain";content-type=null初始化后半段会初始化contentType属性,这个位置会重新设置header的content-type。
3、Content-Type="text/plain";content-type="application/json"实际request请求的时候,会整合dio对象中的baseOptions和request传的options,那么又会出现设置 contentType 的情况,但是在这里会有默认的contentType 为application/json

情况三:初始化后设置options的content-type

1、content-type=null初始化什么都不做
2、content-type="text/plain"初始化后设置header的content-type
3、content-type="text/plain"request请求整合header时会重新拿header里的content-type设置

情况四:初始化后设置options的Content-Type首字母大写

1、content-type=null初始化什么都不做
2、content-type=null;Content-Type="text/plain"初始化后设置header的Content-Type
3、content-type="application/json";Content-Type="text/plain"request请求整合header时会重新拿header里的content-type设置。
****因为顺序问题,后台拿到的是后面的content-type,即是Content-Type="text/plain"

相关文章

网友评论

      本文标题:flutter Dio设置Content-Type的问题

      本文链接:https://www.haomeiwen.com/subject/tezwvktx.html