Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python urllib3 doesn't seem to be sending fields data

I am trying to utilise the authentication here: https://api.graphnethealth.com/system-auth using Python urllib3 and have the following

import urllib3
http = urllib3.PoolManager()
resp = http.request(
        "POST",
        "https://core.syhapp.com/hpca/oauth/token",
        headers={
            "Content-Type": "application/x-www-form-urlencoded"
        },
        fields={
            "grant_type": "client_credentials",
            "client_id": "YYYYYYYYY",
            "client_secret": "XXXXXXXXX"
        }
    )
print(resp.data)

I get an error saying that grant_type has not been sent.

b'{\r\n  "error": {\r\n    "code": "400",\r\n    "message": "Validation Errors",\r\n    "target": "/oauth/token",\r\n    "details": [\r\n      {\r\n        "message": "grant_type is required",\r\n        "target": "GrantType"\r\n      },\r\n      {\r\n        "message": "Value should be one of the following password,refresh_token,trusted_token,handover_token,client_credentials,pin",\r\n        "target": "GrantType"\r\n      }\r\n    ]\r\n  }\r\n}'

Any suggestions?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re telling it the data will be form-urlencoded, but that’s not what request does by default. I believe you need:

resp = http.request(
        "POST",
        "https://core.syhapp.com/hpca/oauth/token",
        fields={
            "grant_type": "client_credentials",
            "client_id": "YYYYYYYYY",
            "client_secret": "XXXXXXXXX"
        },
        encode_multipart = False
    )

request replaces the Content-Type header, so there’s no point in specifying it at all.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading