question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot access response body after request is intercepted by MSW

See original GitHub issue

Environment

Name Version
msw 0.35.0
chrome Version 91.0.4472.114
Ubuntu 20.04.2 LTS

Request handlers

I have the following setup for msw in my project. The request that I’m having an error should be bypassed.

const setup = () => {
  if (typeof window === 'undefined') {
    const { server } = require('./server');
    server.listen();
  } else {
    const { worker } = require('./browser');
    worker.start({ onUnhandledRequest: 'bypass' });
  }
};

Actual request

const parseJSON = (text) => JSON.parse(text);

fetch('/url').then((res) => parseJSON(res.text()))

Current behavior

I get the following error in my application

Uncaught (in promise) TypeError: Failed to execute 'text' on 'Response': body stream already read
    at parseJSON

Expected behavior

I should be able to access the response.text in the promise resolve as the request was bypassed.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
patrickmcdougle-oktacommented, Sep 13, 2021

No. Cloning the repo and manually bumping mswjs/interceptors to the current version worked for me. I need to get a little organized, but I can have a PR ready this week.

1reaction
kettanaitocommented, Sep 8, 2021

Hey, @romaolucas.

We’ve fixed this issue in the browser about half a year ago. What you’re experiencing is likely to be a Node.js issue that was raised in the related library and fixed (https://github.com/mswjs/interceptors/releases/tag/v0.12.7). Please wait for the next MSW release for the fix to land in your project. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Axios never receives mocked response object msw@0.38.1
After reading through #180 and trying out msw@0.17.1 , the issue is ... with GET request but POST just was not intercepted or...
Read more >
Debugging uncaught requests - Recipes - Mock Service Worker
A request to /user or /posts will not be intercepted as they lie outside of the worker's scope ( /assets ).
Read more >
Getting response body of mock service worker - Stack Overflow
The mock response depends on the request. We can return different mock responses based on request url parameters.
Read more >
Getting started with Mock Service Worker - LogRocket Blog
According to its official website, Mock Service Worker is an API mocking library that uses the Service Worker API to intercept actual requests....
Read more >
Mock Service Worker adopts a brand-new request interception ...
If the connection is refused, MSW catches the respective error and silences it until it knows there are no matching request handlers. Once...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found