python+request 发送post请求:msg返回"Content type 'application/octet-stream' not supported"
一、问题源代码:
1、代码:
1 import requests 2 import json 3 url = "http://47.106.203.20:8000/ssposs2/api/auth/login" 4 par = { 5 "account":"ceshi", 6 "pwdMd5":"e10adc3949ba59abbe56e057f20f883e" 7 } 8 r = requests.post(url,data=json.dumps(par))#因为请求传送的参数是josn格式,所以这里要用到json.dumps() 9 print(r.text)#请求返回内容 10 print(r.status_code)#请求返回状态
2、运行结果:
1 {"result":false,"msg":"Content type 'application/octet-stream' not supported","code":"E1000","data":null} 2 200
二、post请求一般有4种请求,分别是:
1、application/x-www-form-urlencoded浏览器原生表单
2、multipart/form-data
3、application/json
4、text/xml文本格式
所以需要加上请求内容类型Content-Type: XXXXX
三、修改后代码和运行结果
1、代码:
import requests import json url = "http://47.106.203.20:8000/ssposs2/api/auth/login" par = { "account":"ceshi", "pwdMd5":"e10adc3949ba59abbe56e057f20f883e" } r = requests.post(url,data=json.dumps(par),headers={'Content-Type':'application/json'}) print(r.text)#请求返回内容 print(r.status_code)#请求返回状态
2、运行结果:
{"result":true,"msg":null,"code":null,"data":"login success. "} 200