首页 > 解决方案 > 将 cURL 转换为 Python:使用额外的参数

问题描述

我正在尝试通过适当的授权发出 API 请求。我查看了将 curl 转换为 python 请求,但在放置授权标头时遇到了麻烦。

这是我到目前为止所拥有的:

import base64
import http.client
import requests
import json

headers = {
        "Content-Type": "application/json",
        'Authorization': 'Basic %s' % base64.b64encode(b'username:password').decode('ascii'),
    }
payload = {json.dumps({"one":["two:three:four"]})}
url = 'https://website/v1'


r = requests.post(url, data=payload, headers=headers)

if __name__=='__main__':
    print (r.text)

上面提供的链接和我的代码之间的唯一区别是他有query={"tags":["test1","test2"]}我根本没有。

这是我试图翻译的卷曲以获得上面的内容。

curl -X POST -u "username:password" -H "Content-Type:application/json" "https://website.com" -d '{"ids":["something:numberOne:numbertwo"]}'

任何帮助表示赞赏。

标签: pythoncurl

解决方案


推荐阅读