[英]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
建議開機自檢。 有關此選項的更多信息,請參見:(11.2) http://curl.haxx.se/docs/httpscripting.html
卷曲輸出:
{"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"})
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.