make a http request with a custom body as form data in javascript

To make a HTTP request with a custom body as form data in JavaScript, you can use the FormData object along with the XMLHttpRequest object.

Here is an example code snippet:

index.tsx
const formData = new FormData();
formData.append('username', 'JohnDoe');
formData.append('password', 'pa$$word');

const xhr = new XMLHttpRequest();
xhr.open('POST', '/api/login');
xhr.send(formData);
201 chars
8 lines

In this example, we create a FormData object and append the form data fields to it. Then, we create a XMLHttpRequest object and set the HTTP method and URL. Finally, we send the FormData object as the request body using the send method of the XMLHttpRequest object.

gistlibby LogSnag