how to use python to execute a curl command
Question: I'm new to python and I want to execute a curl command in python.

Usually, I just need enter the command in terminal and press return key.
However, I don't know how it works in python.

The command shows below:

    
    
    curl -d @request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere
    

There is a request.json file to be sent to get response.

I searched a lot and got confused. I tried to write a piece of code, although
I could not fully understand. It didn't work.

    
    
    import pycurl
    import StringIO
    
    response = StringIO.StringIO()
    c = pycurl.Curl()
    c.setopt(c.URL, 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere')
    c.setopt(c.WRITEFUNCTION, response.write)
    c.setopt(c.HTTPHEADER, ['Content-Type: application/json','Accept-Charset: UTF-8'])
    c.setopt(c.POSTFIELDS, '@request.json')
    c.perform()
    c.close()
    print response.getvalue()
    response.close()
    

The error message is 'Parse Error'.Can anyone tell me how to fix it? or how to
get response from the sever correctly? Thanks a lot!


Answer: For sake of simplicity, maybe you should consider using the standard library
[Requests](http://docs.python-requests.org/).

An example with json response content would be something like:

    
    
    import requests
    r = requests.get('https://github.com/timeline.json')
    r.json()
    

If you look for further information, in the [Quickstart](http://docs.python-
requests.org/en/latest/user/quickstart/) section, they have lots of working
examples.

**EDIT:**

For your specific curl translation:

    
    
    import requests
    import json
    url = 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere'
    payload = json.load(open("request.json"))
    headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}
    r = requests.post(url, data=json.dumps(payload), headers=headers)
    

