Send file as binary while also passing other params to an API

Hi,
I would like to achieve the following:

  • watch a github repo for changes [using Github trigger]
  • get files that were created or changed [use Github node]
  • send this file to an API that accepts files as well as other params for content translation [use HTTP request to send to Taia.io]

I managed to get the github part working well. From there I take the file and store it using Write binary file. Then using Read Binary files I try to pass these files to the API using the HTTP Request node.

I figured out how to send binary files as part of the JSON/RAW parameters → Body Content type: Form-data Multipart → Send Binary data toggle (as described here HTTP Request - n8n Documentation

But I’m having issues figuring out how to send both, API request body params as well as the file, similar to how I can do that in Postman:
Screenshot 2022-07-21 111531

Any help with this is greatly appreciated!

Hi @Matija_Kovac, welcome to the community :tada:

This is a bit tricky at the moment I am afraid as mixing binary and non-binary data isn’t currently supported in the HTTP request node (but the team is already working on making this possible in the next version of this node).

Currently, you would need to write a bit of code in the Function node for this, this post has a quick example:

Hope this helps!

Hi @MutedJam , thanks for sharing this solution. It took me a while to get everything set up but this answer led me to the right solution eventually that seems to work now!

The tricky part was getting the binary files across but I managed somehow.
Thanks so much!

1 Like

Glad to hear this worked out in the end, thanks so much for confirming!

Hey @MutedJam,

just a note here for anyone else attempting this (solution posted below):

  • include - NODE_FUNCTION_ALLOW_EXTERNAL=request-promise-native in your environment variables (in my case a docker-compose file)

I’m trying to move this forward to a server and one thing I’m missing from this is to store the response from the API request.

This is how my script looks like now:

const request = require('request-promise-native');
var binaryData = items[0].binary.dataOut;
const data = await this.helpers.getBinaryDataBuffer(0, 'dataOut');

var options = {
  'method': 'POST',
  'url': 'https://stagebackend.taia.io/api/v1/project/create/',
  'headers': {
    'Authorization': 'Bearer <bearer token is here>
  },
  formData: {
    'name': 'test',
    'source_language': 'fr-FR',
    'target_language': 'de-DE',
    'files[]': {
      'value': Buffer.from(data, 'base64'),
      'options': {
        'filename': binaryData.fileName,
        'contentType': binaryData.mimeType
      },
    }
  }
};

try {
  await request(options);
  return [{ json: {} }];
} catch (e) {
  return [{ json: {
      error: JSON.stringify(e)
    }
  }];
}

It’s the try/catch block that returns an empty result for me

Instead I expect the API to return a new project ID that was created with this post request and that is precisely what I can confirm when testing this endpoint on postman.
Now, I’m aware that this isn’t an n8n question specifically, and may be to do with the block itself, but I tried a bunch of solutions and none of them returns a valid response.

Thanks in advance for helping me out!

Hey @Matija_Kovac,

The first thing I notice is the Try / Catch is set to return nothing which could be why you are not seeing anything, The other missing bit is you are not setting a variable to contain the response from the request.

As a quick test I would probably do something like the below then check the browser console on run to see what it outputs you can then use the result in the return and all should be good.

try {
  let result = await request(options);
  console.log(result);
  return [{ json: {} }];
} catch (e) {
  return [{ json: {
      error: JSON.stringify(e)
    }
  }];
}

Hey @Jon ,

thanks a bunch, somehow I didn’t see that function missing. This solves my issue!
Now off to figuring out how to store this data so I can later lookup the status of these translation projects.

Hey @Matija_Kovac,

When it comes to storing data there are a bunch of options, There is workflow static data which is handy for smaller amounts of data or for larger items you can use any of the database nodes we have. Personally I am a big fan of using Baserow or Redis.