Let us understand what does json() method do in JavaScript. We use fetch API for GET, POST, PUT and DELETE requests in JavaScript. json() method is very commonly used when the request is POST. json() method takes JSON as input and parses it to produce a JavaScript object. It doesn't take any parameters and returns a promise that resolves into a JavaScript object. The object can be an array, a string, a number, or an object.
Whenever a client sends an HTTP request to the server using the POST method, the server will respond with one response. The response is commonly referred to as res. Since it is a single response, calling it again in the same request throws TypeError: Failed to execute 'json' on 'Response': body stream already read.
So now, how do we solve this? There exists a clone() method for allowing multiple uses of the body object. Hence we can read a response more than once in sequence. It creates a copy of the current request object.
Top comments (0)