-1
const fd = new FormData();
fd.append('name1', 'value1');
fd.append('name2', 'value2');
fd.append('name3', new Blob([`zzzzzzzzzzzzzzzzz`]));
const xhr = new XMLHttpRequest();
xhr.open(`POST`, location.href);
xhr.send(fd);

in devtools network panel, request body string is

------WebKitFormBoundaryTUux0YL1tLSIGtnn
Content-Disposition: form-data; name="name1"

value1
------WebKitFormBoundaryTUux0YL1tLSIGtnn
Content-Disposition: form-data; name="name2"

value2
------WebKitFormBoundaryTUux0YL1tLSIGtnn
Content-Disposition: form-data; name="name3"; filename="blob"
Content-Type: application/octet-stream

zzzzzzzzzzzzzzzzz
------WebKitFormBoundaryTUux0YL1tLSIGtnn--

image

so How to convert FormData to request body string ?

const formDataToString = (fd: FormData): string => {
  // TODO
  return ``;
};

1 Answer 1

2

I find we can use Request, but it is asynchronous

const fd = new FormData();
fd.append('name1', 'value1');
fd.append('name2', 'value2');
fd.append('name3', new Blob([`zzzzzzzzzzzzzzzzz`]));
const req = new Request(location.origin, {
  method: `POST`,
  body: fd,
});
const td = new TextDecoder('utf-8');
console.log(td.decode(await req.arrayBuffer()));

Is there a way to do this without async/await ?

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.