python - 烧瓶上的 POST 请求失败
问题描述
我正在尝试从这个页面重现代码,完整的 github 代码在这里:
该应用程序在浏览器上运行良好,但我无法从 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'
解决方案
添加Content-Type
到您的帖子标题中以指定您要发送 JSON 数据:
requests.post(url,data=json.dumps(PEOPLE), headers={'Content-Type': 'application/json'})
您还可以使用该json
参数来实现相同的结果:
requests.post(url, json=json.dumps(PEOPLE))
推荐阅读
- angular - 在属性上使用输入和输出装饰器时获取事件对象
- javascript - Jasmine 单元测试:错误:找不到模块“parser.js”
- node.js - Chrome 在终端中打开或由 webriver 运行时不呈现字体
- c# - Visual Basic 中的数据库连接和填充列表框
- ruby-on-rails - 我的 Rails 应用程序返回 500,但我在日志中看不到任何详细信息
- rust - 我怎样才能接受可以被`..`切片的任意容器?
- azure - [Azure Web Sever + Unity]:身份验证问题
- php - 带有 Recaptcha v2 的 PHP 表单 - 无需提交即可刷新表单
- c# - 在 ASP .NET Core 应用程序中“无法加载 Microsoft.VisualBasic.Tristate”
- laravel - 如何从表 Laravel 中仅查询 1 级和 2 级用户列表