求助: 怎么把这个 curl 命令用 python requests 搞定?

2013 年 5 月 20 日
 y
命令如下:

curl -X POST -d '{ "method" : "test", "params" : [ { "detail" : "Hello_world"} ] }' http://example.com

在 bash 里面执行是能得到想要的结果的。但希望在 python 里面搞。

其中 data 的部分是 { "method" : "test", "params" : [ { "detail" : "Hello_world"} ] }

我不太清楚怎么用 python requests 处理 [ { "detail" : "Hello_world"} ] 这个部分。

现在的做法是 os.system 写到一个文件里,再把文件里的内容读出来,囧。
17091 次点击
所在节点    Python
5 条回复
mengzhuo
2013 年 5 月 20 日
确实囧得很
json模块不行吗?
Jet
2013 年 5 月 20 日
import httplib
import json

p = { "method" : "test", "params" : [ { "detail" : "Hello_world"} ] }
headers = {"Content-type":"application/x-www-form-urlencoded","Accept":"text/plain"}
conn = httplib.HTTPConnection("127.0.0.1", 8081)
conn.request("POST","", json.dumps(p), headers)

不知道理解是否正确。
hit9
2013 年 5 月 20 日
In [1]: import requests

In [2]: data = {
...: 'method': 'test',
...: 'params': [
...: {'detail':'hello world'}
...: ]
...: }

In [3]: requests.post("http://example.com",data=data)
Out[3]: <Response [200]>
xyly624
2013 年 5 月 20 日
mmnsghgn
2017 年 7 月 15 日
推荐一个网站: https://curl.trillworks.com/

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://v2ex.xtra.eu.org/t/69444

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX