post请求的body数据类型和content-type的关系

背景:登陆接口的类型是post,request接口的content-type是multipart/form-data; boundary=----WebKitFormBoundaryxeYAwSy6FSo4kow9
response接口的content-type是application/json; charset=utf-8
image

接口的请求体
image

在编写python脚本时post接口的请求头content-type定义了类型multipart/form-data; boundary=----WebKitFormBoundaryxeYAwSy6FSo4kow9
请求体data使用的数据结构是
{"accounts": "admin","pwd": "zlf123456","type": "username"}
或者
'{"accounts": "admin","pwd": "zlf123456","type": "username"}'
都校验失败,提示登陆类型有误

尝试删除content-type之后data使用{"accounts": "admin","pwd": "zlf123456","type": "username"}
可以校验成功
抓包查看接口请求自动带上了content-type=application/json; charset=utf-8
image
image

但是如果定义了content-type=application/json; charset=utf-8
data={"accounts": "admin","pwd": "zlf123456","type": "username"}就校验失败了
image

定义了content-type=application/json; charset=utf-8
data='{"accounts": "admin","pwd": "zlf123456","type": "username"}'
image

不定义content-type
json= {"accounts": "admin","pwd": "zlf123456","type": "username"}
image

不定义content-type
json= '{"accounts": "admin","pwd": "zlf123456","type": "username"}'
image

总结

content-type 头部告诉对方实体主体的内容类型,以便对方能够正确地解析和处理数据。
定义了content-type,请求内容的类型就要和content-type保持一致
如果没有定义content-type,根据请求体的内容来猜测媒体类型,或者使用默认值
请求体采用json=请求内容,会自动转换为json格式,不需要在请求头指定content-type=application/json; charset=utf-8
前提是请求内容不是json格式(字符串),不然也会失败,会转换2次
posted @ 2024-09-23 11:42  吐司波妞  阅读(150)  评论(0)    收藏  举报