Missing request body when handling a POST via superagent
See original GitHub issueEnvironment
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:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you for the follow-up. One of the issues in the example above was that indeed I should not pass
req.body
toctx.body
ifreq.body
isn’t a string. I probably triedctx.body
instead ofctx.json
because the latter was not giving me any properties. Starting from 0.20.0, usingctx.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. 👍
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.