Api python

I tried to use the 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 "" PUT {"status":"ok","shortenedUrl":"http:\/\/\/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


source to share

2 answers

I prefer using python lib called requests ( ) 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 . 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("", {"urlToShorten":""}, headers={"public-api-token": "your_api_token"})
print response.content
>>> {"status":"ok","shortenedUrl":"http:\\/\\/\\/ryHyU"}
import json
decoded_response = json.loads(response.content)
print decoded_response
>>>{u'status': u'ok', u'shortenedUrl': u''}




And to print only the generated url use ...

import requests
import json

response = requests.put("", {"urlToShorten":""}, headers={"public-api-token": "85d3636f48c112de6e413865afc177b5"})
decoded_response = json.loads(response.content)




All Articles