Shorte.st Api python

I tried to use the shorte.st api to automatically generate my short links using my python program, but I really don't know how to use Apis! The dedicated page only has this code:

curl H "public-api-token: ---" -X -d "urlToShorten=google.com" PUT http://api.shorte.st/v1/data/url {"status":"ok","shortenedUrl":"http:\/\/sh.st\/XXXX"}

      

In public-api-token, I have to explicitly insert my personal token, but since curl is for c (I think), how can I use them with python? Many thanks

+3


source to share


2 answers


I prefer using python lib called requests ( http://docs.python-requests.org/en/latest/ ) for http requests. All you have to do is submit the url you would like to abbreviate as dict data and your public api token in headers under the key "public-api-token". You can find the api token at https://shorte.st/tools/api . The response content comes in as a json encoded string, so you need to decode it to get a dict object.



import requests
response = requests.put("https://api.shorte.st/v1/data/url", {"urlToShorten":"google.com"}, headers={"public-api-token": "your_api_token"})
print response.content
>>> {"status":"ok","shortenedUrl":"http:\\/\\/sh.st\\/ryHyU"}
import json
decoded_response = json.loads(response.content)
print decoded_response
>>>{u'status': u'ok', u'shortenedUrl': u'http://sh.st/ryHyU'}

      

+4


source


And to print only the generated url use ...



import requests
import json

response = requests.put("https://api.shorte.st/v1/data/url", {"urlToShorten":"google.com"}, headers={"public-api-token": "85d3636f48c112de6e413865afc177b5"})
decoded_response = json.loads(response.content)
print(decoded_response['shortenedUrl'])

      

0


source







All Articles