Skip to content
Advertisement

I can’t send my POST method to the API with Barer Token

I’m new to API Rest and I’m currently trying to POST to Azure Health Data Services using Python with Databricks. To do this I need to get the token which I get through Postman without problems (in Postman my POST does work).

The data I send are these (it does work in Postman):

data = """
{"resourceType":"Patient","identifier":[{"use":"official","type":{"coding":[{"system":"http://terminology.hl7.org/CodeSystem/v2-0203","code":"NNcol"},{"system": "https://registraduria.gov.co/identificaciones","code": "CC","display": "Cedula de Ciudadania"}]},"value":"DSA2FSA1319832-K"}]}
"""

Then the statement I have in python to send the POST is this:

import requests
from requests.structures import CaseInsensitiveDict

url = 'https://workspace-test-2.fhir.azurehealthcareapis.com/Patient/'

token = 'eyJ0eXAiOiJKV1QiLCJ'

headers = CaseInsensitiveDict()
headers["Accept"] = "application/json"
headers["Authorization"] = "Bearer " + token,  
headers["Content-Type"] = "application/json"

resp = requests.post(url, headers=headers, data=data)

print(resp.status_code)

This returns a error:

InvalidHeader: Value for header {Authorization: ('Bearer eyJ0eXAiOiJKV1QiLCJ',)} must be of type str or bytes, not <class 'tuple'>

It is worth mentioning that the TOKEN is generated on the fly, so I make sure it is valid to send the POST.

The truth is that I don’t know what I’m doing wrong or what I’m missing to make my POST correctly.

I will be attentive to your answers, thank you very much.

Regards.

Advertisement

Answer

The issue is that in this line headers["Authorization"] = "Bearer " + token, you end it with a comma. In that way, python will add a tuple to the key “Authorization” and not a string. Hence, removing the comma should do the trick

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement