File Upload¶
Some Endpoints allow uploading files, which requires generating a presigned URL, which can be used to push a file and the url is used during a subsequent call. To name a few: Creating Batches/Transactions, uploading Attachments, submitting Reconciliation Data.
In essence, users can choose from three distinct methods to relay data:
- Direct input into the
datakey. - By uploading a structured JSON file and using the
urlkey (Depending on the enpoint). - By uploading a raw file and using the
urlkey (Depending on the enpoint).
Prerequisites¶
The code snippets provided in this scenario require the following variables:
BASE_API_URL- The target GIB API domain, which can be found in the API-Reference.API_KEY- The API key setup by the client for using the API. Can be generated using the Dashboard.
Step 1 - Get an Upload URL¶
Make a GET call to the file/upload_url endpoint and pass in the file extension as the content_type parameter. This call does not require the API Key as authorization.
import requests
from os.path import splitext
file_path = "test.pdf"
content_type = splitext(file_path)[1].lstrip(".")
resp = requests.get(f"{BASE_API_URL}/file/upload_url", params={"content_type": content_type}, headers={'Authorization': f"{API_KEY}"})
upload_url = resp.json()["url"]
Step 2 - Read and Upload File¶
Read in the file to be uploaded as binary data and send that as the request data for a PUT call to the URL acquired in Step 1.
Use the same URL from Step 1 again as the url in the POST data for the endpoint.