Skip to content

Mock a request with a dynamic query parameter #2059

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

You must be logged in to vote

Hi, @kr8n3r. That's a great question.

As the warning suggests, you shouldn't put search parameters in the request handler URLs. Those will have no effect.

Instead, to match the requests you want, provide the path to the resource and access the value of the q search parameter in the response resolver. Then, decide how to handle the request based on that parameter's value.

http.get('/v2/user_provided_service_instances', ({ request }) => {
  const url = new URL(request.url)
  const q = url.searchParams.get('q')

  // Return a mocked JSON response if the "q"
  // search parameter equals to a specific value.
  if (q === 'space_guid:ABC-123') {
    return HttpResponse.json({ mocked: true })
  }
})

Replies: 4 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by kettanaito
Comment options

You must be logged in to vote
1 reply
@kettanaito
Comment options

Comment options

You must be logged in to vote
1 reply
@kettanaito
Comment options

Comment options

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

@sanks
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #2058 on February 28, 2024 17:23.