Reputation: 123
I am making a program and I have to translate a Curl command to python, I know how to do requests but not a complicated Curl command like this one so I have no idea how to do it. Can someone help me?
Curl command:
curl -H "public-api-token: 59d46c97de11677e7b23750da01ff6a5" -X PUT -d "urlToShorten=google.com" https://api.shorte.st/v1/data/url
It would be much appreciated if someone wants to translate it for me and wants to explain to me how to do it.
Upvotes: 0
Views: 98
Reputation: 1146
import requests
my_url = "https://api.shorte.st/v1/data/url"
my_data = {"urlToShorten": "google.com"}
my_headers = {"public-api-token":"59d46c97de11677e7b23750da01ff6a5"}
response = requests.put(my_url, my_data, headers=my_headers)
print(reponse.json())
Upvotes: 0
Reputation: 379
you can use requests
module in python:
import requests
r = requests.put('https://api.shorte.st/v1/data/url', data = {'urlToShorten':'google.com'}, headers={'public-api-token' : '59d46c97de11677e7b23750da01ff6a5'})
status_code=r.status_code # get status code
res=r.json() # get response as json
Explaining the code:
1) url
is https://api.shorte.st/v1/data/url
2) you need to pass a Public API token in the headers so you will provide the headers
arg as a dict (see above).
3) finally, the payload is specified in the data
arg as shown in the above example.
requests
module is quite useful, you may check out the docs here:
https://requests.readthedocs.io/en/master/user/quickstart/
Upvotes: 2