在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. Spark读取HDFS文件,任务本地化(NODE_LOCAL)

    Spark也有数据本地化的概念(Data Locality),这和MapReduce的Local Task差不多,如果读取HDFS文件,Spark则会根据数据的存储位置,分配离数据存储最近的Execu ...

  2. STM32点亮LED

    原理图 测试灯,接GPIO外设B,Pin 12 举例 前提,工程模版建立好 #include "stm32f10x.h" void delay(u32 i) { while(i-- ...

  3. android适配知识总结

    一.http适配 背景:API升级到28以后,不再支持明文的网络请求,只支持https请求.运行所报错误:java.net.UnknownServiceException: CLEARTEXT com ...

  4. MySQL时间类型及获取、展示处理

    MySQL时间格式 mysql所支持的日期时间类型有:DATETIME. TIMESTAMP.DATE.TIME.YEAR. 几种类型比较如下: 日期时间类型 占用空间 日期格式 最小值 最大值 零值 ...

  5. git 常用命令操作

    目录 一.用户和邮箱 用户和邮箱的作用 查看用户名和邮箱地址 修改用户名和邮箱地址 用户名和邮箱地址的作用 用户名和邮箱地址是本地git客户端的一个变量,不随git库而改变. 每次commit都会用用 ...

  6. 第三章 Lambda表达式

    第三章 Lambda表达式 3.1 函数式编程思想概述 在数学中,函数就是有输入量.输出量的一套计算方案,也就是“拿什么东西做什么事情”.相对而言,面向对象过分强调“必须通过对象的形式来做事情”,而函 ...

  7. PAT Basic 1063 计算谱半径 (20 分)

    在数学中,矩阵的“谱半径”是指其特征值的模集合的上确界.换言之,对于给定的 n 个复数空间的特征值 { , },它们的模为实部与虚部的平方和的开方,而“谱半径”就是最大模. 现在给定一些复数空间的特征 ...

  8. Kinect视频中运用全身运动和人体测量统计学的人物识别技术

    摘要: 对于人物识别技术来说,动作和人体测量统计学对于光学差异并不敏感,甚至对于眼镜,头发,帽子的描述相当粗糙,现在的以步态为基础的识别技术都是基于对细节的精确描述和对步态周期的精确测量.这种方法需要 ...

  9. [原创]在Windows平台使用msys2、mingw64和vscode编写和调试C/C++代码

    相关名词就不解释了,这里主要讲讲在vscode里怎么配,这里假设大家相关工具已经装好. 题外话:里面的大多数坑都是windows平台和linux平台的差异造成的,如果在linux平台配置,应该会顺利很 ...

  10. MyBatis日期用法技巧

    当你想在实体类中使用Java.util.Date类型,而且还想在数据库中保存时分秒时,你可以在xml中修改为: #{xxdate,jdbcType=TIMESTAMP} 就是将#{}中的jdbcTyp ...