在flutter中在http请求发送时设置"content-type": "application/json"会出现报错Cannot set the body fields of a Request with content-type “application/json”

请求如下:

final putResponse = await http.put('http://192.168.201.21/user/modifyUser',
body: putData,
headers: {"token": userBasicsInfo.userTokenResult,"content-type": "application/json"}
).then((response){
var backResult;
if(response.statusCode == 200){
Utf8Decoder utf8decoder = Utf8Decoder();
backResult = json.decode(utf8decoder.convert(response.bodyBytes));
}else{
print('数据请求错误:${response.statusCode}');
}
return backResult;
});

  

请求发送之后会出现如下报错

E/flutter ( 7295): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Bad state: Cannot set the body fields of a Request with content-type "application/json".

  

解决方法

通过jsonEncode处理要提交的参数

final putData = jsonEncode(params);    // 处理提交参数
final putResponse = await http.put('http://192.168.201.21/user/modifyUser',
body: putData,
headers: {"token": userBasicsInfo.userTokenResult,"content-type": "application/json"}
).then((response){
var backResult;
if(response.statusCode == 200){
Utf8Decoder utf8decoder = Utf8Decoder();
backResult = json.decode(utf8decoder.convert(response.bodyBytes));
}else{
print('数据请求错误:${response.statusCode}');
}
return backResult;
});

  

处理之后再提交即可成功

注意:

[body]设置请求的正文。它可以是[String],[List]或[Map]。如果它是一个String,则使用[encoding]进行编码,并将其用作请求的主体。请求的内容类型将默认为“text / plain”。

如果[body]是List,它将用作请求正文的字节列表。

如果[body]是Map,则使用[encoding]将其编码为表单字段。请求的内容类型将设置为"application/x-www-form-urlencoded"; 这不能被覆盖。[encoding]默认为[utf8]。

处理flutter http请求添加application/json报错Cannot set the body fields of a Request with content-type “application/json”的更多相关文章

  1. Jmeter发送post请求报错Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

    常识普及: Content-type,在Request Headers里面,告诉服务器,我们发送的请求信息格式,在JMeter中,信息头存储在信息头管理器中,所以在做接口测试的时候,我们维护Conte ...

  2. jmeter报"msg":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported"的解决方法

    1.报"msg":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supporte ...

  3. Content type 'application/json;charset=UTF-8' not supported异常的解决过程

    首先说一下当时的场景,其实就是一个很简单的添加操作,后台传递的值是json格式的,如下图 ,后台对应的实体类, @Data @EqualsAndHashCode(callSuper = false) ...

  4. ajax使用向Spring MVC发送JSON数据出现 org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported错误

    ajax使用向Spring MVC发送JSON数据时,后端Controller在接受JSON数据时报org.springframework.web.HttpMediaTypeNotSupportedE ...

  5. IDEA导入maven中导入net.sf.json报错的解决方法

    使用IDEA搭建Maven项目导入架包时, 添加net.sf.json的jar包的时候,代码如下: 在pom.xml文件时: <dependency> <groupId>net ...

  6. SpringBoot报错:Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

    Spring Boot报错:Error starting ApplicationContext. To display the conditions report re-run your applic ...

  7. springboot 报错 Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

    开始 controller 方法写的是 @RequestMapping( value = "/add", method = RequestMethod.POST ) public ...

  8. Springboot实体类转JSON报错Could not find acceptable representation & 设置访问项目根路径的默认欢迎页面

    =================实体类转JSON报错的解决办法============= 之前在springmvc的时候也报过这个错,原因以及springmvc中解决办法参考:https://www ...

  9. fasterxml.jackson 将对象转换为json报错处理

    最近在做查询的数据遇到如下报错: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found ...

随机推荐

  1. 6.SpringMVC 配置式开发-处理器

    处理器除了实现Controller 接口外,还可以继承自一些其他的类,来完成一些特殊的功能 1.继承自AbstractController类 若处理器继承自AbstractController类,那么 ...

  2. 我理解的epoll(一)——实现分析

    epoll项目中用了几次,但是对于其原理只是一知半解.我希望通过几篇blog能加深对她的理解. 我认为epoll是同步IO,因为他在调用epoll_wait时,内核在有I/O就绪前是阻塞的,虽然可以将 ...

  3. php的小数位数最长多少位

    在php中, echo 0.1234567890123456;exit; // 结果为:0.12345678901235, 整数部分为0时,最多到14位小数,如果后面还有,就自动四舍五入 echo 7 ...

  4. QTP10破解步骤(亲试有效)

    QTP10破解步骤: 1.安装qtp,一路默认下来(一定要默认安装目录),一直到要求输入License的界面 2.拷贝mgn-mqt82.exe到C:\Program Files\Mercury In ...

  5. PhpStorm添加PHP代码规范检查CodeSniffer(phpcs)和PHP代码静态分析工具Mess Detector(phpmd)

    一.安装 添加镜像,加速下载 ./composer.phar  config -g repo.packagist composer https://packagist.phpcomposer.com ...

  6. 【异常】 Ensure that config phoenix.schema.isNamespaceMappingEnabled is consistent on client and server.

    1 详细异常 ror: ERROR 726 (43M10): Inconsistent namespace mapping properties. Ensure that config phoenix ...

  7. 电脑设置自动关机和取消自动关机代码bat命令

    一.设置定时关机bat 1.首先在桌面新建一个记事本txt 2.打开记事本,输入“shutdown -s -t 1260 ”1260是指1260秒,即21分钟内关机.可以修改成别的时间. 3.保存后, ...

  8. [SHOI2013]阶乘字符串

    题目描述 给定一个由前\(n\)个小写字母组成的串\(S\). 串\(S\)是阶乘字符串当且仅当前\(n\)个小写字母的全排列(共\(n!\)种)都作为\(S\)的子序列(可以不连续)出现. 由这个定 ...

  9. requests模拟浏览器请求模块初识

    requests模拟浏览器请求模块初识  一.下载 requests模拟浏览器请求模块属于第三方模块 源码下载地址http://docs.python-requests.org/zh_CN/lates ...

  10. SpringMVC——问题汇总

    1.html页面ajax请求/login.后台报: Request method 'GET' not supported 原因: 后台请求使用了method = POST,但是url请求没有formd ...