将Python HTTP请求转换为curl

时间:2019-03-26 22:30:18

标签: python json http curl request

我在Python中有以下代码,该代码正在向OAuth2令牌发出POST请求。它使用基本身份验证。 该代码可以正常工作,但我想对其进行“翻译”以使其卷曲。

代码:

#Authorization: Basic c29tZV91c2VyOnBhc3M=
#some_user:pass = base64decode('c29tZV91c2VyOnBhc3M=') 

def get_access_token():
burp0_url = "https://myurl:443/api/oauth/token"
burp0_headers = {"Accept": "application/json", "Authorization": "Basic c29tZV91c2VyOnBhc3M=", "Content-Type": "application/x-www-form-urlencoded", "Connection": "close", "Accept-Encoding": "gzip, deflate", "User-Agent": "okhttp/3.0.1"}
burp0_data={"grant_type": "client_credentials"}
return json.loads(requests.post(burp0_url, headers=burp0_headers,
data=burp0_data).text)['access_token']

我的猜测是它看起来像这样:

curl -v -XPOST -H 'Authorization: Basic c29tZV91c2VyOnBhc3M=' --header 'Accept: application/json' --header 'Connection: close' --header 'Accept-Encoding: gzip, deflate' --header 'User-Agent: okhttp/3.0.1' --data '{"grant_type": "client_credentials"}' https://myurl:443/api/oauth/token

但是我不断获得HTTP / 1.1 400和以下版本
*书写体失败(0!= 10)
*写入数据失败
*停止了暂停流!
*关闭连接0

你能帮我吗?

1 个答案:

答案 0 :(得分:0)

您好像忘记了将"Content-Type": "application/x-www-form-urlencoded"标头复制到curl命令中。

这也将建议数据不像您当前正在作为JSON字符串提交,而是作为常规表单数据提交。您可以为此使用-F 'grant_type=client_credentials'并删除--data参数。