Trying to upload a file to an HTTP endpoint

I have the request that has to be made which I am able to make from Postman but am unsuccessful in doing from HTTP node.

I looked at this and was trying to send it as binary:
Webhook to HTTP request but was unsuccessful. I am getting an error that the endpoint isn’t getting any file from the request.

curl --location --request POST 'https://endpoint.something.com/upload' \
--header 'Authorization: something' \
--form '[email protected]"record.csv"' \
--form 'user_email="[email protected]"' \
--form 'recipe_slug="recipe_slug_test"'

I am using Binary > data from the HTTP node after downloading the file.

image

I think I might be missing something really small.

Thanks

When using multipart/form-data in the HTTP Node you can just send either binary data or key-value pairs in the body. Does the API allow you to upload the file first and later update the record with the user_email and recipe_slug ?

If so, then you can update the file first by setting up the HTTP node as follow:

and then, with the Record ID, you can make probably a PUT with the user_email and recipe_slug in the body.

Unfortunately, it doesn’t allow for that. I will write a function node to do so and see if I can access the internet from the function node.

Do you think there might be workaround possible for this? We are moving away from Zapier and this is a blocker for a few scenarios.

ah yes, you can use a function node and make the HTTP Request from there. This works as long as you are self-hosting.

Set the env variable export NODE_FUNCTION_ALLOW_BUILTIN=request-promise-native

then in the function node, require the request module and made the HTTP Request using multipart/form-data. If you need help with the function node let me know.

You can check the post below as well.

I tried using axios with export and getting this error:

Is this not the correct way of using export in the function node?

@Veer_Manhas The export option needs to be set via CLI (in npm based setup) or .env file (in Docker based setup), after installation of the node modules, appropriately, that need to be exported.

See:

3 Likes

Thanks! This helps. Very new to n8n.

Did that and tried AXIOS to call the API endpoint. I have to wait and then return data but for some reason n8n is saying

ERROR: No data got returned. Always return an Array of items!

var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');
var data = new FormData();
data.append('file', fs.createReadStream('3eVqeBxtm/recLKtWD7SpurvZwP (1).csv'));
data.append('user_email', '[email protected]);
data.append('recipe_slug', 'recipe_slug_test');

var config = {
  method: 'post',
  url: 'https://endpoint.api',
  headers: { 
    'Authorization': 'auth-code', 
    ...data.getHeaders()
  },
  data : data
};

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
  let respData = [];
  respData.push(response.data);
  return respData;
})
.catch(function (error) {
  console.log(error);
});

Yes, because by the time the request finishes the function already returned. You might want to do something like below instead.

const response = await axios(config)
return [
    {
       json: response.data
   }
]