如何从python请求模块构造curl命令? [英] how to construct the curl command from python requests module?
问题描述
Python requests 是一个很好的模块,可以简化我的 Web REST API 访问编程,我通常会像下面这样
Python requests is a good module to ease my web REST API access programming, I usually do like below
import json
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
当出现错误时,我想看看它背后发生了什么.构建curl
命令在命令行中重现是常用的方式,因为这是RESP API文档中描述最多的标准方式
And when there is error comes out, I want to see what happen behind it. Constructing the curl
command to reproduce in command line is the common way, since this is the standard way which is most described in RESP API document
try:
r = requests.post(url, data=json.dumps(payload), headers=headers)
except Exception as ex:
print "try to use curl command below to reproduce"
print curl_request(url,"POST",headers,payload)
我可以为此请求生成 curl
命令示例会很好,请参阅 中的好示例libcloud的debug,找不到简单的构造方法,下面是我想自己创建的方法.
It will be nice I can generate curl
command sample for this request, see good example in libcloud's debug, I can't find a simple way to construct, below are the method I want to create by myself.
# below code is just pseudo code, not correct
def curl_request(url,method,headers,payloads):
# construct curl sample from requests' structure
# $ curl -v -H "Accept: application/json" -H "Content-type: application/json"
# -d '{"some":"data"}'
# -X POST https://api.github.com/some/endpoint
request = "curl -v "
for header in headers:
print header
request = request + '-H "' + header + ": " + headers[header] + '" '
for payload in payloads:
request = request + '-d {} "' + payload + ": " + payloads[payload] + '" '
request = request + "-X %s %s" % (method,url)
return request
如果我们已经在 requests
中有方法也很好
It will also be nice if we have method in requests
already
以下是得到答案的最终解决方案,对我有用.在这里展示以供您参考
Below are the final solution get the answer, works for me. Show it here for your reference
def curl_request(url,method,headers,payloads):
# construct the curl command from request
command = "curl -v -H {headers} {data} -X {method} {uri}"
data = ""
if payloads:
payload_list = ['"{0}":"{1}"'.format(k,v) for k,v in payloads.items()]
data = " -d '{" + ", ".join(payload_list) + "}'"
header_list = ['"{0}: {1}"'.format(k, v) for k, v in headers.items()]
header = " -H ".join(header_list)
print command.format(method=method, headers=header, data=data, uri=url)
推荐答案
这个方法曾经存在于请求中,但它远非与模块远程相关.您可以创建一个接受响应并检查其 request
属性的函数.
This method existed in requests once upon a time but it is far from being remotely relevant to the module. You could create a function that takes a response and inspects its request
attribute.
request
属性是一个 PreparedRequest
对象,因此它具有 headers
和 body
属性.正文是您使用 -d
传递给 curl 的内容,并且可以像上面一样生成标题.最后,您需要从 request
对象中取出 url
属性并发送它.除非您使用自定义身份验证处理程序执行某些操作,否则钩子对您无关紧要.
The request
attribute is a PreparedRequest
object so it has headers
, and body
attributes. The body is what you pass to curl with -d
and the headers can be generated as you did above. Finally you'll want to pluck off the url
attribute from the request
object and send that. The hooks don't matter to you unless you're doing something with a custom authentication handler.
req = response.request
command = "curl -X {method} -H {headers} -d '{data}' '{uri}'"
method = req.method
uri = req.url
data = req.body
headers = ['"{0}: {1}"'.format(k, v) for k, v in req.headers.items()]
headers = " -H ".join(headers)
return command.format(method=method, headers=headers, data=data, uri=uri)
那应该有效.您的数据将被正确格式化,无论是 multipart/form-data
还是其他任何形式.
That should work. Your data will be properly formatted whether it is as multipart/form-data
or anything else.
这篇关于如何从python请求模块构造curl命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!