简体   繁体   English

将 curl 命令转换为 request.post

[英]Converting curl command to request.post

I have following curl command:我有以下 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

And i am trying to do the same over python using requests:我正在尝试使用请求对 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

i am able to get the result from command line.我能够从命令行获得结果。 but when i tried with request.post its failing.但是当我尝试使用 request.post 时,它失败了。 it says:它说:

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"

returned as result.text: on the website作为结果返回。文本:在网站上

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

can any one suggest me why my python post request is failing when printed as json?谁能建议我为什么我的 python 发布请求在打印为 json 时失败? How to convert response.text to json object如何将 response.text 转换为 json object

Some websites require encoded data.一些网站需要编码数据。 So, have you tried:那么,您是否尝试过:

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

or, using the json parameter:或者,使用json参数:

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

In the latter case, payload will be automatically encoded.在后一种情况下,有效载荷将被自动编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM