python - 将 CURL Post 转换为 Python 请求失败

标签 python curl server python-requests

我无法成功地将 curl post 命令转换并执行到 python 代码。

curl 命令

curl -X POST -H "Content-Type:application/json; charset=UTF-8" -d '{"name":joe, "type":22, "summary":"Test"}' http://url

转换后的代码

import requests
import json 

url="http://url"
data = {"name":joe, "type":22, "summary":"Test"}
headers = {'Content-type': "application/json; charset=utf8"}
response  = requests.post (url, data=json.dumps(data), headers=headers)
print response.text
print response.headers

我没有得到任何回应,当我从 shell 手动执行它时它工作正常,但是当我执行代码时,没有任何反应,我没有看到错误或任何东西。

最佳答案

如果您使用的是最新版本的请求之一: 尝试使用“json”kwarg(无需显式转换为 json)而不是“data”kwarg:

response = requests.post(url, json=data, headers=headers)

注意:另外,通过这种方式您可以省略“Content-type” header 。

关于python - 将 CURL Post 转换为 Python 请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39706735/

相关文章:

java - 为什么从浏览器下载的文件已损坏?

linux - 在启动时运行命令

php - 如何在我的网站上显示我的Facebook Wall

python - 如何从列中提取子字符串

php - cURL 未获取预期的 POST 响应 - 错误 405

linux - 运行 curl 命令时需要绕过 Location

node.js - 如何让 NodeJs 项目在端口 3000 上可公开访问?

python - 当列表值为字符串时,如何将 else 与列表理解一起使用?

python - TwythonStreamer 重音编码? - 无法解码响应,无效 JSON,代码为 200

PHP header 图像类型输出与位置重定向到 fs 中的二进制文件