Categories
JavaScript Answers

How to fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch?

Spread the love

Sometimes, we want to fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch.

In this article, we’ll look at how to fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch.

How to fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch?

To fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch, we call clone on the response object before we use the response object multiple times.

For instance, we write

const res = await fetch("yourfile.json");
const data = await res.clone().json();

to call res.clone before we call json to clone the response object before we return a promise with the JSON response body.

Conclusion

To fix Failed to execute ‘json’ on ‘Response’: body stream is locked error with JavaScript fetch, we call clone on the response object before we use the response object multiple times.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *