Search code examples
pythonjsonbashcurlpycurl

Regular JSON Curl to pyCurl


I'd like to get this curl command to run in a Python Script

curl -H "public-api-token: 049cxxxxxc026ef7364fa38c8792" -X PUT -d "urlToShorten=google.com" https://api.shorte.st/v1/data/url

but I have now clue how.


Solution

  • def make_tiny(url):
        connection = httplib.HTTPSConnection('api.shorte.st', 443)
        connection.connect()
        connection.request('PUT', '/v1/data/url', json.dumps({
               "urlToShorten": url,
             }), {
               "public-api-token": "049cd75ad7axxx26ef7364fa38c8792",
               "Content-Type": "application/json"
             })
        results = json.loads(connection.getresponse().read())
        return results.get("shortenedUrl", "none").decode('utf-8')
    

    Thanks anyway!