将curl命令转换为request.post

问题描述 投票:1回答:1

我有以下curl命令:

curl.exe -u sys_dcmsys:sys_******1 -d "grant_type=client_credentials&scope=Token_WindowsAuth" -H "Content-type: application/x-www-form-urlencoded" https://abcd-i.company.com/token -k -i

而且我正在尝试使用请求在python上做同样的事情:

payload = {
        'grant_type': 'client_credentials',
        'scope': 'Token_WindowsAuth'
    }
header = {'Content-Type': 'application/x-www-form-urlencoded'}
result = requests.post(
        https://abcd-i.company.com/token,
        auth=HTTPBasicAuth('sys_dcmsys', 'sys_******1'),
        data=payload,
        headers=header,
        verify=False)

res_obj = result.json()
Result = namedtuple('Result', ['access_token', 'expires_in'])
result = Result(
        res_obj['access_token'], res_obj['expires_in'])
expires_in = result.expires_in
access_token = result.access_token

我能够从命令行获取结果。但是当我尝试使用request.post失败时。它说:

ERR Starting new HTTPS connection (1): iamws-i.intel.com:443
2019-10-23T11:48:28.58+0530 [APP/PROC/WEB/0] ERR https://abcd-i.company.com:443 "POST /api/api/v1/token HTTP/1.1" 500 1208
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR Internal Server Error: /isso/auth
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR Traceback (most recent call last):
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR   File "/home/vcap/deps/1/python/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR     response = get_response(request)
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR   File "/home/vcap/deps/1/python/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR     response = self.process_exception_by_middleware(e, request)
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR   File "/home/vcap/deps/1/python/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in _get_response
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR     response = wrapped_callback(request, *callback_args, **callback_kwargs)
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR   File "/home/vcap/app/isso/views.py", line 75, in auth
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR     res_obj = result.json()
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR   File "/home/vcap/deps/1/python/lib/python3.5/site-packages/requests/models.py", line 897, in json
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR     return complexjson.loads(self.text, **kwargs)
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR   File "/home/vcap/deps/1/python/lib/python3.5/json/__init__.py", line 319, in loads
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR     return _default_decoder.decode(s)
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR   File "/home/vcap/deps/1/python/lib/python3.5/json/decoder.py", line 339, in decode
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR   File "/home/vcap/deps/1/python/lib/python3.5/json/decoder.py", line 357, in raw_decode
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR     raise JSONDecodeError("Expecting value", s, err.value) from None
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] ERR json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
2019-10-23T11:48:28.60+0530 [APP/PROC/WEB/0] OUT 10.109.72.31 - - [23/Oct/2019:06:18:28 +0000] "GET /isso/auth HTTP/1.1" 500 27 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"

作为结果返回。文本:在网站上

{
access_token: "XXXXXXXXXXXXXXXXX",
token_type: "Bearer",
expires_in: 5400
} 

有人建议我为什么当打印为json时我的python post请求失败?如何将response.text转换为json对象

python django curl python-requests cloudfoundry
1个回答
0
投票

[某些网站需要编码数据。因此,您是否尝试过:

requests.post(..., data=json.dumps(payload), ...)

或使用json参数:

requests.post(..., json=payload, ...)

在后一种情况下,有效载荷将被自动编码。

© www.soinside.com 2019 - 2024. All rights reserved.