Search code examples
python-3.xcurllibcurlpycurlshlex

How to execute a curl command in Python and get the response and pass it through other code


I have a curl command. I want to execute in in Python and fetch the response to pass it through other code.

curl https://api.box.com/oauth2/token -d 'grant_type=refresh_token' -d 'refresh_token=Ew38UXVKS3kc0axFt6MdDklUnoHpipxTzDWBmKlXAG9ImvGafbbaUhQndv89e677' -d 'client_id=3qkm5h4wh765b3khiws0z8hdpkc56jhs' -d 'client_secret=h9AeXzZL3KATJuaHmimFFBRBDZQrp9tr' -X POST

How can I execute the script in Python and get the response and pass it through other code?

When I am executing the curl script in CMD, I am getting this response:

{"access_token":"uz843jpIiEWnu0CcuT9as2XbA3UEQTR67","expires_in":4261,"restricted_to":[],"refresh_token":"GsDaP6VyUpHN8vDHbz9ktAjLfMLN0dFL6PMIK4fmDH8eKRqR360vDhQTBhIMZxy67","token_type":"bearer"}

From the above response I need to take the access_token value.


Solution

  • Like avloss said - try out requests.

    Another great resource is the application Postman

    It will let you try out any http calls you'd like, and then can also translate that into/out of curl and/or python requests code for you (and a bunch of other languages).

    I find it really useful when trying to figure how to use requests for anything more than simple http calls.