0

In my browser side script, I want to get the entire POST request payload stream.

But when the body is a FormData object, especially when it contains a file blob, is there any easy way to make it?

The reason for doing this is that I want to make AES encrypt on the whole request body, using axios request interceptor.

For example:

I want to convert the FormData object:

const fd = new FormData()
fd.append('example.png', file) // here file is a file object from the file input object

Into the below content:

------WebKitFormBoundaryMV9GYQ2pcwRJ6XAA
Content-Disposition: form-data; name="image"; filename="example.png"
Content-Type: image/png
<<blob bytes>>

------WebKitFormBoundaryMV9GYQ2pcwRJ6XAA--

Is there any easy way to make it or any exists npm packages?

2
  • Does this answer your question? Javascript formdata: encrypt files before appending Commented Feb 24, 2021 at 8:16
  • Thank you, but I think it may be a bit inconvinient if I implement the whole procedure myself. I'd rather expecting a exists library to make it. Commented Feb 24, 2021 at 8:23

1 Answer 1

4

I am not sure if it will really help your end goal, but for what you ask, you can create a new Response object from this FormData and consume it as plain text:

(async () => {
  const file = await new Promise((res) => document.createElement('canvas').toBlob(res));
  const fd = new FormData();
  fd.append('example.png', file, 'example.png');
  const resp = new Response(fd);
  console.log( await resp.text() );
})();

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.