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

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.