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.

Missing request body when handling a POST via superagent

See original GitHub issue

Environment

Name Version
msw 0.19.5
node 12.18.2
OS Windows 10

Request handlers

    // just replicates the request data back to the client
    rest.post('http://mock.example.net:8080/foo', (req, res, ctx) => {
        return res(
            ctx.status(200),
            ctx.body(req.body),
        )
    })

Actual request

const superagent = require('superagent')

let response = await superagent.post('http://mock.example.net:8080/foo')
    .send({
        bar: 6,
        baz: '34',
    })

expect(response.body).toMatchObject({
    bar: 6,
    baz: '34',
})

Current behavior

Requests made via superagent are intercepted, but req.body at the respective response resolver always comes out as an empty string, regardless of the contents provided in the HTTP request. This was verified by peeking req in a debugger. Full Jest test below. The equivalent request using http.request directly does not reproduce the problem.

Expected behavior

I would have expected req.body to contain the request’s data in the HTTP message body (in the example, an object {bar: 6, baz: '34'}, although the behavior is not specific to this object nor to a JSON payload). The request itself is well formed and behaves well in other contexts (example programs to a non-mocked server appear to work fine).

Test

const superagent = require('superagent')
const { rest } = require('msw')
const { setupServer } = require('msw/node')

const MOCKED_URL = 'http://mock.example.net:8080/foo'
let server = setupServer(
    rest.post(MOCKED_URL, (req, res, ctx) => {
        return res(
            ctx.status(200),
            ctx.body(req.body),
        )
    }),
)

beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close())

test('send POST request with body via superagent', async () => {
    let response = await superagent.post(MOCKED_URL)
        .send({
            bar: 6,
            baz: '34',
        })

    expect(response.body).toMatchObject({
        bar: 6,
        baz: '34',
    })
})

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
Enet4commented, Aug 10, 2020

Thank you for the follow-up. One of the issues in the example above was that indeed I should not pass req.body to ctx.body if req.body isn’t a string. I probably tried ctx.body instead of ctx.json because the latter was not giving me any properties. Starting from 0.20.0, using ctx.json would work as expected.

I eventually stumbled upon another issue related with the use of cookies in environments without document (#312), but it has since been resolved and released in 0.20.2 (#313).

Consider this resolved. 👍

0reactions
kettanaitocommented, Aug 10, 2020

Could you please update to msw@0.20.x and let me know if the issue is still reproducible? If so, please share a reproduction scenario with us (in your fork, for example) so we could take a look. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

post request using superagent - node.js - Stack Overflow
I have been trying to make a post request to the pokeapi using superagent request lib. I am not sure why the request...
Read more >
superagent - npm
Small progressive client-side HTTP request library, and Node.js module with the same API, supporting many high-level HTTP client features.
Read more >
How to use the superagent.post function in superagent - Snyk
To help you get started, we've selected a few superagent.post examples, ... request .post(msg.content.url) .send({event: msg.content.event}) .end((err3, ...
Read more >
superagent - npm.io
superagent -serializer - Converts server payload into different cases; superagent-httpbackend - stub out requests using AngularJS' $httpBackend syntax ...
Read more >
superagent | Yarn - Package Manager
end(…) . Consider not using .end() at all, and migrating to promises by calling .then() instead. In Node, responses with unknown MIME type...
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