![]() |
REST API
16
REST API for Luna Network HSMs
|
The REST API supports file input and output. This allows you to send and receive files within requests and responses.
When receiving a file, the response object will contain the contents of the file in a buffer that can then be iterated and saved to a file.
Example:
r = requests.get("/api/lunasa/webServer/config/csr",
stream=True,
cookies=cookies,
verify=False,
allow_redirects=False)
with open("ssl.csr", 'wb') as csr:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
csr.write(chunk)
An alternative way to receive a file is to provide the request 'Accept' header with the value "application/octet-stream". This is not always required; the method above accounts for most cases. Some resources, however, may return json as well as octet-stream, in which case the 'Accept' header is required.
Example:
headers["Accept"] = "application/octet-stream"
r = requests.get("/api/lunasa/partitionPolicyTemplates/myTemplate",
cookies=cookies,
verify=False,
headers=headers,
allow_redirects=False)
with open("template.csv", 'wb') as csr:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
csr.write(chunk)
To send a file, the header Content-Type needs to be set to 'octet-stream' to notify the server that it will be receiving a file. In python, passing the file object to the data parameter is all that is required.
Header format:
headers = {'Content-Type': "application/vnd.safenetinc.lunasa+octet-stream;version="}
Example:
with open(filename, 'rb') as payload:
r = requests.put("/api/lunasa/webServer/config/certificate",
stream=True,
cookies=cookies,
data=payload,
header=headers,
verify=False,
allow_redirects=False)