Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with Query Params path-to-regexp #483

Open
Scilor3 opened this issue Sep 1, 2023 · 1 comment
Open

Problems with Query Params path-to-regexp #483

Scilor3 opened this issue Sep 1, 2023 · 1 comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@Scilor3
Copy link

Scilor3 commented Sep 1, 2023

Using query parameters in the URL of a route does not work. The documentation recommends to read the path-to-regexp doc. Here it is described that the question mark (?) must be escaped: https://www.npmjs.com/package/path-to-regexp#modifiers

As described I escaped the question mark: /os/api/ecs/search\\?search=01.7541914201.0002.18

However, I get a 404 response back from the mock api server. Am I doing something wrong here or missing something?

To reproduce the bug, it is enough to set up a minimal setup, with the following route:

{ id: 'get-search', url: '/os/api/ecs/search\\?search=01.7541914201.0002.18', method: 'GET', variants: [ { id: 'default', type: 'json', options: { status: 200, body: defaultSearch, }, }, ], }

and call the URL in the browser, e.g. http://localhost:3000/os/api/ecs/search?search=01.7541914201.0002.18

The following appears in the debug logs:
11:22:11:47 [debug][server] Request received | GET => /os/api/ecs/search?search=01.7541914201.0002.18 | Assigned id: 3ed515d7-b7ef-4593-832d-1bd1ed12479d
11:22:11:47 [debug][server] Sending Not found response | GET => /os/api/ecs/search?search=01.7541914201.0002.18 | 3ed515d7-b7ef-4593-832d-1bd1ed12479d
11:22:11:47 [error][server] Sending Error 'Not Found' | 3ed515d7-b7ef-4593-832d-1bd1ed12479d

I am using the following software versions:
npm: 9.5.0
Node: v18.14.2

If you are missing some information, please let me know.

Thanks for your help and best regards

@javierbrea
Copy link
Member

Hi @Scilor3 , you're right, the server does not support defining different route variants for specific query params. This is because it uses express under the hood, and it does not consider query strings as part of the route path. So, for the moment you should remove the query param from the route path, and, if you want to send different responses based on the query params, then you should use amiddleware route variant, in which you could get the query params through req.query and act in consequence.

Anyway, this is something that should be explained better in the documentation, and maybe we can also investigate how to support defining query params in the route paths out of the box.

@javierbrea javierbrea added documentation Improvements or additions to documentation enhancement New feature or request labels Oct 8, 2023
@javierbrea javierbrea moved this to To do in Backlog May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
Status: To do
Development

No branches or pull requests

2 participants