Tiny Frontend Logo
Posts 0131

On This Page

Customising JavaScript fetch()

In our previous post, we explained about how the fetch API can be used in JavaScript to make GET and POST requests. Let's look at some of the more advanced concepts of the fetch API in JavaScript.

Request Object

In our previous posts and in most scenarios, the fetch API is used as:

fetch('https://tinyfrontend.com/example/submit',{
    method: 'POST',
    body: data
})

You can simplify and make this code more reusable using a Request object instead. The Request constructor has the following signature:

new Request(input: Request | string, options?: RequestInit)
  • The first argument is the resource URL. Or you can pass another Request object to make a copy and reuse it in another request.

  • The second argument is the init object where you can pass custom config parameters to modify the request.

The init object accepts the following properties:

  • body
  • cache
  • credentials
  • headers
  • integrity
  • keepalive
  • method
  • mode
  • priority
  • redirect
  • referrer
  • referrerPolicy
  • signal

You might have noticed that the Request constructor accepts the same arguments as the fetch API!

Let's look at an example of how this can be useful.

const baseRequest = new Request('https://tinyfrontend.com/friends',{
    headers: myHeaders,
    credentials: encryptedCredentials
})

const getRequest = new Request(baseRequest, {
    method: 'GET'
})

const postRequest = new Request(baseRequest, {
    method: 'POST',
    body: data
})

fetch(getRequest).then(...)
fetch(postRequest).then(...)

Headers Object

The fetch API also provides a Headers interface which can be used to set and reuse headers across multiple requests. The Headers interface allows us to set, add, remove or iterate through the headers in a particular request.

To create a Headers object, simply call the constructor:

const customHeaders = new Headers();

Then, you can add or delete custom headers to this object and reuse it in your requests.

myHeaders.append("Authorisation", encryptedToken);
myHeaders.append("Content-Type", "application/json");
myHeaders.append("X-Custom-Header", "secret-key");
myHeaders.append("X-for-endpoint1", "value");

fetch("/api/v4/endpoint1",{
    headers: customHeaders
})

myHeaders.delete("X-for-endpoint1");

fetch("/api/v4/endpoint2",{
    headers: customHeaders
})
Subscribe to read the full content

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

Read Next

0130

Making a POST Request to the Server

In our previous post, we wrote about GET requests using JavaScript's fetch() method. This is a simple use case where we pass in a url into the method. However, fetch() also takes in a second parameter (optional) where we can pass in various request options.

0129

Data Fetching from the Server

Do you know what AJAX is? It seems to be an "old" concept today, it allows us to use JavaScript to send asynchronous data requests to the server and update the rendering of the page locally.

Data Fetching from the Server
0128

JavaScript Currying

The first time I heard the term Currying was during a job interview preparation. Fortunately, the interviewer did not ask me any questions about it.

JavaScript Currying
0127

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.

FormData in JavaScript
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