Skip to main content
Question

Why Is My JavaScript FormData Object Empty?

  • October 23, 2024
  • 0 replies
  • 21 views

Forum|alt.badge.img

I’m trying to send a file to an API via JavaScript within the Platform UI (not CLI).  I’m trying to avoid the Platform CLI for some specific reasons, but understand that may be the only solution.

The problem I can’t figure out is how after I append 2 key/value pairs to the myFormData object, it ends up empty when sent via z.request().  I want the myFormData object to have my 2 keys and their values, plus set the content-type header to multipart/form-data with the appropriate boundary.

Note, the JavaScript File object returns a not found error.  Any help or ideas would be really appreciated.

 

bundle.inputData.documentation_file is a stashed file (previously hydrated)

bundle.inputData.documentation_file_name is just a string of the file name

const pdfFileParts = await(z.request({url: bundle.inputData.documentation_file, raw: true}));
const pdfBlob =  new Blob(await(pdfFileParts.buffer()), {type: "application/pdf"});

const myFormData = new FormData();
myFormData.append("file", pdfBlob, bundle.inputData.documentation_file_name);
myFormData.append("metadata", JSON.stringify({
	"fileName": bundle.inputData.documentation_file_name,
	"folderCheckByApp":"<redacted>",
	"bucket":"<redacted>",
	"documentName":"<redacted>"
}));

var file_options = {
	url: '<redacted>',
	method: 'POST',
	skipThrowForStatus: true,
	headers: {
		//'content-type': 'multipart/form-data',
		'Accept': 'application/json'
	},
	body: myFormData,
	removeMissingValuesFrom: {
		'body': false,
		'params': false
	}
};

const response = await(z.request(file_options));

 

Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.