0
  • I'm using node fetch to make API Calls inside a platform.
  • I need to make an API Call to pass file in form-data.
  • Below is my stub code:
const fetch = require("node-fetch")
var myHeaders = {"Authorization": "Basic Y2hhdEJvdDpJRkxjYkAxMjM="
,'cache-control': 'no-cache'
,"content-type": "multipart/form-data;"
};

let file_content = "base 64 file content";

let getFormDataForWhatsAppStatement = (data,fileContent,fileExt)=>{
  let jsonData = { "data":{ "templateName":"Test_123", "fieldName":"Document_Purpose,Policy_Number,Document_Category", "fieldValue":`AttachDocument, ${data.policyNumber}, Customer_Requirement`, "docList":"Test_Doc" } }
  let formDataPairs = [];
  let finalFormData = '';

  formDataPairs.push(encodeURIComponent("") + '=' + encodeURIComponent(jsonData));
  formDataPairs.push(encodeURIComponent("") + '=' + encodeURIComponent(fileContent));

  finalFormData = formDataPairs.join('&').replace(/%20/g, '+');

  return finalFormData;
}

let formdata = getFormData({"policyNumber":"006558144"},file_content,"png");

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: formdata
};


fetch(url, requestOptions)
  .then(response => response.json())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));
  • The error I get here is boundary parameter is not defined.
  • So I removed the content-type header as stated in the below thread: Boundary Issue

  • But then it gives me connection timeout error(since the request format is incorrect).

  • So is there a way to create formData Similar to the below code without using FormData Object?

const FormData = require('form-data');
const fetch = require("node-fetch")
var formdata = new FormData();
var myHeaders = {"Authorization": "Basic Y2hhdEJvdDpJRkxjYkAxMjM="
//,"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryyEmKNDsBKjB7QEqu"
};

formdata.append("", "{ \n   \"data\":{ \n\n      \"templateName\":\"Test_123\",\n      \"fieldName\":\"Document_Purpose,Policy_Number,Document_Category\",\n      \"fieldValue\":\"AttachDocument, G0000784, Customer_Requirement\",\n      \"docList\":\"Test_Doc\"  \n}\n}\n");
formdata.append("", "base 64 file data", "close.png");
var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: formdata,
  redirect: 'follow'
};

fetch(API_URL, requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

1 Answer 1

1

Instead of literally inputting the boundary have you tried using the form-data function getBoundary() and assigning it to a variable like so:

let boundary = formdata.getBoundary();
var myHeaders = {"Authorization": "Basic Y2hhdEJvdDpJRkxjYkAxMjM="
"Content-Type": `multipart/form-data; boundary=${boundary}`
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.