Skip to content

Why the req.url.searchParams parameters object is empty when I test two get requests using Mock Service Worker? #910

Answered by kettanaito
E01T asked this question in Q&A
Discussion options

You must be logged in to vote

Hey, @E01T.

First thing to clarify, your example feature two distinct things:

  • Query parameters: /search/users?a=1&b=2 ("a" and "b");
  • Path parameters: /users/:id ("id").'

Path parameters are exposed under req.params object and are available based on the parameter name you give after the semicolon:

rest.get('/users/:id', (req, res, ctx) => {
  req.params.id // where "id" is taken from ":id"
})

Query parameters are exposed under a URLSearchParams instance, which is not an object. To access a query paramete you must call the .get() method on the said instance and provide it with the query parameter name:

rest.get('https://api.github.com/search/users', async (req, res, ctx) => {
  // given "…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@E01T
Comment options

@kettanaito
Comment options

Answer selected by E01T
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
bug Something isn't working scope:node Related to MSW running in Node
2 participants
Converted from issue

This discussion was converted from issue #909 on September 12, 2021 18:21.