Skip to main content
 首页 » 编程设计

python之将 curl 命令转换为 request.post

2024年11月01日9傻小

我有以下 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 
}  

谁能告诉我为什么我的 python post 请求在打印为 json 时失败了? 如何将response.text转成json对象

请您参考如下方法:

一些网站需要编码数据。那么,您是否尝试过:

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

或者,使用 json 参数:

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

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