I can't get data with python API REST

Hello group. I’m testing Orthanc’s Api Rest, but I’m having trouble using / tools / find. With curl (on linux or windows) it works fine, but not with python. Code:

import requests
from requests.auth import HTTPBasicAuth
data = ‘{“Level”:“Study”,“Query”:{“PatientName”:“BRAINIX”},“Expand”:true}’
url = “http://localhost:8042/tools/find -d”
response = requests.get(url + ’ '’ +data + '' ', auth=HTTPBasicAuth(‘alice’, ‘luisPassword’))

print(response.text)

Response:
{
“HttpError” : “Not Found”,
“HttpStatus” : 404,
“Message” : “Unknown resource”,
“Method” : “GET”,
“OrthancError” : “Unknown resource”,
“OrthancStatus” : 17,
“Uri” : "/tools/find -d ‘{"Level":"Study","Query":{"PatientName":"BRAINIX"},"Expand":true}’ "
}

with curl (on linux):
curl -u alice:alicePassword http://localhost:8042/tools/find -d ‘{“Level”:“Study”,“Query”:{“PatientName”:“BRAINIX”},“Expand”:true}’
and curl on windows:
curl -u alice:alicePassword http://localhost:8042/tools/find -d ‘{"Level":"Study","Query":{"PatientName":"BRAINIX"},"Expand":true}’

Response (OK):
{
“ID” : “27f7126f-4f66fb14-03f4081b-f9341db2-53925988”,
“IsStable” : true,
“LastUpdate” : “20210804T012231”,
“MainDicomTags” :
{
“AccessionNumber” : “0”,
“InstitutionName” : “7GEFF0GbzqCNo43Yd0,Ibu,zQSSX”,
“ReferringPhysicianName” : “dAEvNTxZJO0E”,
“RequestedProcedureDescription” : “IRM c\u00e9r\u00e9brale, neuro-cr\u00e2ne”,
“StudyDate” : “20061201”,
“StudyDescription” : “IRM c\u00e9r\u00e9brale, neuro-cr\u00e2ne”,
“StudyID” : “218211405”,
“StudyInstanceUID” : “2.16.840.1.113669.632.20.1211.10000357775”,
“StudyTime” : “141645.000000”
},
“ParentPatient” : “16738bc3-e47ed42a-43ce044c-a3414a45-cb069bd0”,
“PatientMainDicomTags” :
{
“PatientBirthDate” : “19490301”,
“PatientID” : “5Yp0E”,
“PatientName” : “BRAINIX”,
“PatientSex” : “0000”
},
“Series” :
[
“1e2c125c-411b8e86-3f4fe68e-a7584dd3-c6da78f0”
],
“Type” : “Study”
}
]

Please, I need help.
Thank You

Hello,

Here is a full working sample:

import json
import pprint
import requests
from requests.auth import HTTPBasicAuth

data = {
“Level”:“Study”,
“Query”:{“PatientName”:“BRAINIX”},
“Expand”:True
}

response = requests.post(‘http://localhost:8042/tools/find’,
json.dumps(data),
auth = HTTPBasicAuth(‘alice’, ‘luisPassword’))

pprint.pprint(json.loads(response.content))

Regards,
Sébastien-

Thank You very very much Sébastien.
It works very well.
Saludos.
Luis

Please excuse me again with another question.
But I want to list the studies of a patient, but I can’t do it. Please could you help me.
Code:

import pprint
import json
import requests

from requests.auth import HTTPBasicAuth
uspw = HTTPBasicAuth(‘alice’, ‘luisPassword’)

data = {
“Level”:“Study”,
“Query”:{“PatientName”:“BRAINIX”},
“Expand”:True
}

response = requests.post(‘http://localhost:8042/tools/find’,
json.dumps(data),
auth = uspw)

response_json = json.loads(response.json)
pprint.pprint(response_json)

for patient in response_json[‘ID’]:
print(‘Study Date:’, patient[‘LastUpdate’])
print(‘Name:’, patient[‘PatientName’])
print(‘Study:’, patient[‘StudyDescription’])
print(‘DOB:’, patient[‘PatientBirthDate’])
print(‘’)

Result:
TypeError: list indices must be integers or slices, not str

Thank you

Here is a corrected version of your script (modifications are highlighted in bold):

import pprint
import json
import requests

from requests.auth import HTTPBasicAuth
uspw = HTTPBasicAuth(‘alice’, ‘luisPassword’)

data = {
“Level”:“Study”,
“Query”:{“PatientName”:“BRAINIX”},
“Expand”:True
}

response = requests.post(‘http://localhost:8042/tools/find’,
json.dumps(data),
auth = uspw)

response_json = json.loads(response.content)
pprint.pprint(response_json)

for patient in response_json:
print(‘Study Date:’, patient[‘LastUpdate’])
print(‘Name:’, patient[‘PatientMainDicomTags’][‘PatientName’])
print(‘Study:’, patient[‘MainDicomTags’][‘StudyDescription’])
print(‘DOB:’, patient[‘PatientMainDicomTags’][‘PatientBirthDate’])
print(‘’)

Thank you. You are very kind.
The code worked perfectly.