我正在编写一个程序,我必须将Curl命令转换为python,我知道如何执行请求,但不知道像这样的复杂的Curl命令,因此我不知道该怎么做。有人能帮我吗?
卷曲命令:
curl -H "public-api-token: 59d46c97de11677e7b23750da01ff6a5" -X PUT -d "urlToShorten=google.com" https://api.shorte.st/v1/data/url
如果有人想为我翻译它并想向我解释如何做,将不胜感激。
最佳答案
您可以在python中使用requests
模块:
import requests
r = requests.put('https://api.shorte.st/v1/data/url', data = {'urlToShorten':'google.com'}, headers={'public-api-token' : '59d46c97de11677e7b23750da01ff6a5'})
status_code=r.status_code # get status code
res=r.json() # get response as json
解释代码:
1)
url
是https://api.shorte.st/v1/data/url2)您需要在标头中传递公共API令牌,以便提供
headers
arg作为字典(请参见上文)。3)最后,如上例所示,在
data
arg中指定有效负载。requests
模块非常有用,您可以在此处查看文档:https://requests.readthedocs.io/en/master/user/quickstart/