将带有 -X 选项的 curl 命令转换为 python 请求

Translate curl command with -X option to python requests

我正在尝试将以下 curl 命令转换为 python,但它总是返回 <Response 400> 状态代码。这是我试过的:

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

据我理解应该等同于:

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

我尝试了 curl 命令本身,它运行得非常完美。我怀疑问题是笨拙的 -X PUT 选项,尽管 -d 建议 POST。有关此选项的更多信息可在此处找到:(11.2) http://curl.haxx.se/docs/httpscripting.html

curl 的输出:

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

执行 print result 时 python 的输出:

<Response [400]>

尝试使用字典 data:

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