My application is using the following request’s handling chain:
Nginx Server -> Nest.js HTTP controller -> Nest.js service and PostgreSQL connection
.
It supports sending parameters for filtering results in the following way: the filter
parameter is sent, which is an array of objects:
hostname.com/posts?filter=[{"property":"title","operator":"=","value":"RAW234"}]
This construction with operator
property is further transformed into the SQL query like:
WHERE title is 'RAW234'
. I would like to allow users to send ‘ILIKE’ operator to filter result with wildcard. My filter parameter will be: filter=[{"property":"title","operator":"ilike","value":"%234"}]
and here I got a problem – my app is not able to parse filter
query parameter with JSON.parse()
method further in the request processing chain.
I find that my Nginx server receives all queries as:
GET hostname.com/posts?filter=[{%22property%22:%22title%22,%22operator%22:%22ilike%22,%22value%22:%22RAW234%22}]
– this is an expected behavior – the browser encodes special characters in the request.
Next, the filter
parameter comes to the Nest HTTP controller
in the proper form: '[{"property":"title","operator":"ilike","value":"RAW234"}]'
and I can parse it with JSON.parse()
without any issues.
But if I use %
symbol in the value
property as: [{"property":"title","operator":"=","value":"RAW%"}]
, my Nest HTTP controller
receives it as [{%22property%22:%22title%22,%22operator%22:%22=%22,%22value%22:%22RAW%%22}]
– "
symbols encoded to %22
– this is not a valid JSON and JSON.parse()
returns an error.
Only this special character %
breaks this handling chain – with any other ^ & ($#@
it work without problems.
My Nginx config for this route looks like:
location ~ ^/posts/?(.*) {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://$post_microservice_backend/posts/$1$is_args$args;
}
Nginx forwards external requests to HTTP-controller in post_microservice_backend
HTTP-server which is powered by Nest.js.
- Is it possible to receive
filter
parameter as[{"property":"title","operator":"ilike","value":"RAW%"}]
when I use%
symbol? - Maybe it is my Nginx settings issue?
- I tried to use
decodeURIComponent
but it can not parse invalid string. Of course, I can use regular expressions and similar methods to transform a string to get a valid value, but maybe I can change some setting so that it works right away?