Upload File via HTTP Request

Hello,
I am trying to download a file (usually pdf) from a URL. After that I want to upload the same file through an API again. I have got the following “solution” from the API developer - however, I have no idea how to do this in n8n. Can you help me?

I have got the following code:

import requests
 
input_data = {
  "file_url": "[[URL where the file is located]]",
  "api_key": "[[Your API KEY]]",
  "org_id": [[ID of the org that this could be uploaded to]]
 }
 
 # Download the file / hyrate the file
 f_downloaded = requests.get(input_data['file_url'])
 
 # Get the full url of the file
 url = input_data['file_url']
 
 # Get the file name
 filename = url[url.rfind("/")+1:]
 filename_small = filename.replace(".", "_small.")
 
 # Post request to upload the file
 response = requests.post('https://api.affinity.co/entity-files', auth=('',input_data['api_key']), params=({'organization_id': input_data['org_id']}), files={'file': (filename_small, f_downloaded.content, f_downloaded.headers['content-type'])})

Please help. Thank you in advance!

Hi @Silas_Hundhausen, this looks like Python code rather than a generic description of an API endpoint. You might find Affinity’s actual documentation a bit easier.

Essentially you would need to transfer the respective settings to the HTTP Request node. I don’t have an account with this service so can’t test this myself, but it looks like it could be this (more or less):

Example Workflow

You’d need to create basic auth credentials which seem to have no username and your API key as the password. The organization ID would need to be set in the query parameters field.

2 Likes