Tiny Frontend Logo
Posts 0127

On This Page

3. Methods
4. Usage

FormData in JavaScript

The most common way of sending data to the server in a network request is by using a JSON object. But, in some cases, JSON maybe insufficient to transmit the data.

Let's see what are these use-cases and how we can handle them in JavaScript.

Fundamentals

Form data can be used instead of JSON to transmit data like binaries to the server. A network request that uses form data looks something like this:

POST /api/submit HTTP/1.1
Host: tinyfrontend.com
Content-Type: multipart/form-data;boundary="random-hash"

--random-hash
Content-Disposition: form-data; name="field1"

value1
--random-hash
Content-Disposition: form-data; name="field2"; filename="example.txt"

...contents of the file...
--random-hash--

Let's break it down to understand what the different parts mean:

  • Content-Type: This is a request header that specifies that it is a form-data request. It's value in this case will always be multipart/form-data, followed by the boundary.

  • Boundary: This is a random hash to define the boundary for the content so that the server can parse the key-value pairs correctly.

  • Content-Disposition: Provides information about each subpart of the form. Starts with form-data, followed by name which is the key to identify the value with. Different parameters are separated by a semicolon(;).

FormData

The request format shown above looks quite complicated, isn't it? Thankfully, JavaScript provides a FormData API that makes it as easy as creating a JSON object to create the form-data request payload.

To create a FormData object, simply call the constructor:

const form = new FormData();
Subscribe to read the full content

* Free subscribe to enjoy full content access.
* You can cancel anytime with 1-click.

Read Next

0126

HTTP Status Code for SEO

When a browser sends a request, the server responds with a corresponding HTTP status code, whether the request can be fulfilled or not.

HTTP Status Code for SEO
0125

Data Transfer with WebSocket

You're probably already familiar with using the HTTP protocol to retrieve data from servers. However, in addition to HTTP, browsers also provide another networking protocol to meet more advanced communication needs: WebSocket.

Data Transfer with WebSocket
0124

Permissions on a Webpage

Your website cannot readily use some services like camera, microphone, location, etc. on the user device. For security reasons, the websites need to specifically request for permissions from the user to use these services. In this post, let's learn how to handle these permissions on a webpage.

Permissions on a Webpage
0123

Network Requests in JavaScript

One of the crucial and fundamental component of a website is to make Network Requests to fetch some data. There are a lot of libraries available now, like axios, that make this task very easy to implement for the developers. But, if you've ever wondered what goes on underneath those libraries or if you want to achieve this using pure JavaScript, keep reading.

Network Requests in JavaScript
0122

Iterators and Generators in JavaScript

Let's discover some hidden gems and not so commonly used concepts in JavaScript. Iterators and Generators bring the concept of iteration directly into the core language and provide a mechanism for customizing the behavior of for...of loops.

0121

Lazy Load Offscreen Iframes

As more websites embed YouTube videos into their pages to provide richer content, and it indeed helps users access information more effectively.

Lazy Load Offscreen Iframes