Tengo el siguiente comando 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

Y estoy tratando de hacer lo mismo sobre Python usando solicitudes:

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

Puedo obtener el resultado de la línea de comando. pero cuando lo intenté con request.post está fallando. dice :

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"

Devuelto como result.text: en el sitio web

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

¿Alguien puede sugerirme por qué mi solicitud de publicación de Python falla cuando se imprime como json? Cómo convertir response.text a objeto json

2
geek 23 oct. 2019 a las 09:41

1 respuesta

La mejor respuesta

Algunos sitios web requieren datos codificados. Entonces, ¿has probado:

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

O, usando el parámetro json:

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

En este último caso, la carga útil se codificará automáticamente.

1
m01010011 24 oct. 2019 a las 01:10