我正在尝试从此page复制代码,完整的github代码是here:
该应用程序在浏览器上可以正常工作,但是我无法从python再现POST请求。
我尝试使用浏览器时显示在有效负载上的相同数据
PEOPLE = {"fname": "DDoug",
"lname": "FarDrell"}
url = "http://localhost:5000/api/people"
data = requests.post(url,data=json.dumps(PEOPLE) )
但是我得到以下错误:
data.text
'{\n "detail": "Invalid Content-type (), expected JSON data",\n "status": 415,\n "title": "Unsupported Media Type",\n "type": "about:blank"\n}\n'
我也尝试过这样:
url = "http://localhost:5000/api/people"
data = requests.post(url,data=json.dumps(PEOPLE) )
但出现此错误:
'{\n "detail": "Invalid Content-type (application/x-www-form-urlencoded), expected JSON data",\n "status": 415,\n "title": "Unsupported Media Type",\n "type": "about:blank"\n}\n'
答案 0 :(得分:1)
将Content-Type
添加到您的帖子标题中,以指定您要发送JSON数据:
requests.post(url,data=json.dumps(PEOPLE), headers={'Content-Type': 'application/json'})
您还可以使用json
参数来获得相同的结果:
requests.post(url, json=json.dumps(PEOPLE))