Python 3 has a standard API for making HTTP requests. The host x-raydar.info is already filled here, and so the URL replacement just needs to be filled by the path at the end, depending on which algorithm (Computer Vision or NLP) is being called:
/api/cv
for DICOM files and the Computer Vision algorithm
import http.client
import mimetypes
from codecs import encode
conn = http.client.HTTPSConnection("x-raydar.info")
dataList = []
boundary = 'wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T'
dataList.append(encode('--' + boundary))
dataList.append(encode('Content-Disposition: form-data; name=file; filename={0}'.format('/path/to/file')))
fileType = mimetypes.guess_type('/path/to/file')[0] or 'application/octet-stream'
dataList.append(encode('Content-Type: {}'.format(fileType)))
dataList.append(encode(''))
with open('/path/to/file', 'rb') as f:
dataList.append(f.read())
dataList.append(encode('--' + boundary))
dataList.append(encode('Content-Disposition: form-data; name=key;'))
dataList.append(encode('Content-Type: {}'.format('text/plain')))
dataList.append(encode(''))
dataList.append(encode("<Key goes here>"))
dataList.append(encode('--'+boundary+'--'))
dataList.append(encode(''))
body = b'\r\n'.join(dataList)
payload = body
headers = {
'Content-type': 'multipart/form-data; boundary={}'.format(boundary)
}
conn.request("POST", "<URL path goes here (either "/api/cv" or "/api/nlp")>", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Requests is an additional library and syntactically much simpler than the standard http.client package.
Firstly it must be installed via pip from any CLI
python -m pip install requests
Then it can be used to build a client in fewer lines
import requests
url = "https://x-raydar.info/api/cv"
payload={'key': '<Key goes here>'}
FILEPATH = '/path/to/file' #It has to be a DICOM file
files=[
('file',('file',open(FILEPATH,'rb'),'application/octet-stream'))
]
headers = {}
try:
response = requests.request("POST", url, headers=headers, data=payload, files=files, timeout=10)
print(response.text)
except requests.exceptions.Timeout as err:
print(err)