94

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)

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)

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

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)    

3 Answers 3

132

You can also use curlify to do this.

$ pip install curlify
...
import curlify
print(curlify.to_curl(r.request))  # r is the response object from the requests library.
Sign up to request clarification or add additional context in comments.

2 Comments

Looks like if something like a timeout occurs, curlify won't work (since there's no response object) - any suggestions?
@cdm you could use it on a prepared request that you get from the request object by calling the prepare() method. You need to construct the Request object with a constructor rather than one of the methods that prepares the request and then sends it.
82

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.

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)

That should work. Your data will be properly formatted whether it is as multipart/form-data or anything else.

13 Comments

thank, this is what I want to do in sample code, but it turns to be complicated, since I need to handle different type of data, headers[header] above could be integer like, can u paste your sample codes for me to reference ?
If you're passing integers to requests and not getting exceptions, I don't know what you're doing. I can put some example code into my answer though and it'll work on python versions that requests work on (i.e. 2.6+)
shouldn't command be like: 'curl -X {method} -H "{headers}" -d "{data}" {uri}' (notice I added " and -d)
This does not work if you are using params to Request.
@Ijaz Ur Rahim Nope, he doesn’t. join adds the ` -H ` prefix in between the list elements which would result in the first element not being prefixed. Hence @Nickolay’s code is correct.
|
1

I wrote a module that will help solve this problem.

pip install curlifier

All you need is to import curlify.
For example:

>>> import requests
>>> from curlifier import curlify
>>> body = {'id': 1, 'name': 'Tima', 'age': 28}
>>> r = requests.post('https://httpbin.org/', json=body)
>>> curlify(r)
curl --request POST 'https://httpbin.org/' <...> --header 'Content-Type: application/json' --data '{"id": 1, "name": "Tima", "age": 28}'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.