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
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://unsplash.com/photos/SsKE7n2CasY/download?ixid=MnwxMjA3fDB8MXxhbGx8fHx8fHx8fHwxNjM3NjYyNjY0&force=true&w=640",
        "responseFormat": "file",
        "options": {}
      },
      "name": "Download Image",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "authentication": "basicAuth",
        "requestMethod": "POST",
        "url": "https://api.affinity.co/entity-files",
        "jsonParameters": true,
        "options": {
          "bodyContentType": "multipart-form-data"
        },
        "sendBinaryData": true,
        "binaryPropertyName": "file:data",
        "queryParametersJson": "{\"organization_id\": \"Organization ID here\"}"
      },
      "name": "Upload File",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        650,
        300
      ],
      "credentials": {
        "httpBasicAuth": {
          "id": "58",
          "name": "Dummy Basic Auth"
        }
      }
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Download Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Image": {
      "main": [
        [
          {
            "node": "Upload File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

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.

1 Like