REST API  5.0.0
REST API for SafeNet Network HSMs
File I/O

File I/O

Introduction

The REST API supports file input and output. This allows you to send and receive files within requests and responses.

Receiving Files

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 of receiving a file is to provide the request 'Accept' header with the value 'application/octet-stream'. This is not always required as the method above may account for most cases. However some resources 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)

Sending Files

Sending files requires one minor change to the request. 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)