【问题标题】:Translation complicated curl command to python3将复杂的 curl 命令翻译成 python3
【发布时间】:2020-02-04 17:55:51
【问题描述】:

我正在制作一个程序,我必须将一个 Curl 命令转换为 python,我知道如何执行请求,但不知道像这样的复杂 Curl 命令,所以我不知道该怎么做。有人可以帮我吗?

卷曲命令:

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

如果有人想为我翻译它并想向我解释如何做,我们将不胜感激。

【问题讨论】:

  • 如果有人想为我翻译它并想向我解释如何做,我将不胜感激。 Stack Overflow 不是免费的代码编写/软件开发服务。你有没有尝试过,做过任何研究?查看命令和答案,这似乎微不足道。

标签: python json python-3.x api curl


【解决方案1】:

你可以在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) urlhttps://api.shorte.st/v1/data/url 2) 您需要在标头中传递公共 API 令牌,以便将 headers arg 作为字典提供(见上文)。 3) 最后,payload 在data arg 中指定,如上例所示。

requests 模块非常有用,您可以在这里查看文档: https://requests.readthedocs.io/en/master/user/quickstart/

【讨论】:

    【解决方案2】:
    import requests
    my_url = "https://api.shorte.st/v1/data/url"
    my_data = {"urlToShorten": "google.com"}
    my_headers = {"public-api-token":"59d46c97de11677e7b23750da01ff6a5"}
    response = requests.put(my_url, my_data, headers=my_headers)
    print(reponse.json())
    

    【讨论】:

    • 非常感谢,我相信这也会奏效
    猜你喜欢
    • 2017-12-20
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多