title: Using Python Requests to POST data to REST API
body: <p>I have been working with the FHIR REST API for a while but haven't had any experience with Python. As my first python project I am attempting to create a simple python script that can read and write to an open API. I am able to read but I am stuck on creating a successful POST due to the following: error [TypeError(&quot;unhashable type: 'dict'&quot;)]. I don't fully understand how the python dictionary works and attempted to use a tuple but get the same error.</p>
  <pre><code>import requests #REST Access to FHIR Server
  print('Search patient by MRN to find existing appointment')
  MRN = input(&quot;Enter patient's MRN -try CT12181 :&quot;)
  url = 'http://hapi.fhir.org/baseR4/Patient?identifier='+MRN
  print('Searching for Patient by MRN...@'+url)
  
  response = requests.get(url)
  json_response = response.json()
  
  try:
    key='entry'
    EntryArray=json_response[key]
    FirstEntry=EntryArray[0]
    key='resource'
    resource=FirstEntry['resource']
    id=resource['id']
    PatientServerId= id
    patientName = resource['name'][0]['given'][0] + ' ' +resource['name'][0]['family']
    print('Patient Found')
    print('Patient Id:'+id)
    
    #Searching for assertppointments
    
    url='http://hapi.fhir.org/baseR4/Appointment?patient='+id #fhir server endpoint
  
    #Print appointment data 
  
    print('Now Searching for Appointments...@'+url)
    appt_response = requests.get(url).json()
    key='entry'
    EntryArray=appt_response[key]
    print (f'Appointment(s) found for the patient {patientName}')
    for entry in EntryArray:
      appt=entry['resource']
   #   print('-------------------------')
   #   Date=appt['start']
   #   Status=appt['status'] 
   #   print(appt_response)
     #print ('AppointmentStartDate/Time: ' ,appt['start'])
      print ('Status: ' ,appt['status'])
      print ('ID: ' ,appt['id'])
  
      
    print('Search for open general practice slot?')
    option = input('Enter yes or no: ')
    while not(option == 'yes'):
        print('Please search a different paitent')
        option = input('Enter yes or no: ')    
    url = 'http://hapi.fhir.org/baseR4/Slot?service-type=57' #fhir server endpoint
    print('Searching for General Practice Slot...@'+url)
    slot_response = requests.get(url).json()
    key='entry'
    EntryArray=slot_response[key]
    print ('Slot(s) found for the service type General Practice')
    for entry in EntryArray:
      slot=entry['resource']
      #print('-------------------------')
      #slotDate=slot['start']
      #slotStatus=slot['status'] 
      print (f'SlotID: ' +slot['id'])
      #print (f'Status: ' +slot['status'])    
    print('Book a slot?')
    option = input('Enter yes or no: ')
    while not(option == 'yes'):
        print('Please search a different paitent')
        option = input('Enter yes or no: ')
  #Book slot  
    slotID = input(&quot;Enter slot ID :&quot;)
    url = 'http://hapi.fhir.org/baseR4/Appointment' #fhir server endpoint
    print('Booking slot...@'+url)
    headers = {&quot;Content-Type&quot;: &quot;application/fhir+json;charset=utf-8&quot;}
    data = {{&quot;resourceType&quot;: &quot;Appointment&quot;,&quot;status&quot;: &quot;booked&quot;,&quot;slot&quot;: tuple({&quot;reference&quot;:&quot;Slot/104602&quot;}),&quot;participant&quot;: tuple({&quot;actor&quot;: {&quot;reference&quot;:&quot;Patient/1229151&quot;,&quot;status&quot;: &quot;accepted&quot;}}),&quot;reasonCode&quot;: tuple({&quot;text&quot;: &quot;I have a cramp&quot;})}}
  #fhir server json header content
  #  headers = {&quot;Content-Type&quot;: &quot;application/fhir+json;charset=utf-8&quot;}
    response = requests.post(url=url,headers=headers,data=data)
    print(response)
    print(response.json())
  except Exception as e:
      print ('error' ,[e])
    
     
  </code></pre>
  <p>I was expecting the JSON data to successfully write to the API. I am able to use the same JSON data in Postman to make a call, but I am not as familiar on how this should work within Python.</p>