whiterock
whiterock

Reputation: 300

Translate curl command with -X option to python requests

I am trying to translate the following curl command to python, but it's always giving me back <Response 400> status codes. This is what I've tried:

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

which to my understanding should be equivalent to:

result = requests.put("https://api.shorte.st/v1/data/url",
         data='urlToShorten=google.com',
         headers={
             "public-api-token": "myAPIkey",
         }
)

I tried the curl command itself and it works flawlessly. What I suspect to be the problem is the awkward -X PUT option although -d suggests a POST. More info about this option can be found here: (11.2) http://curl.haxx.se/docs/httpscripting.html

Output of curl:

{"status":"ok","shortenedUrl":"http:\/\/sh.st\/caIXv"}

Output of python when doing print result:

<Response [400]>

Upvotes: 0

Views: 604

Answers (2)

vHalaharvi
vHalaharvi

Reputation: 189

import urllib2
request =  urllib2.urlopen(
            urllib2.Request(
            url=url,
            data={"urlToShorten": "google.com"},
            headers={"public-api-token" : "myAPIkey"})
        )

Upvotes: 0

Alasdair
Alasdair

Reputation: 308909

Try using a dictionary for data:

data = {'urlToShorten': 'google.com'}, 

Upvotes: 2

Related Questions