Reputation: 9443
Im trying to query a record from database using this criteria: VITAMINS + ZINC 100MG/10MG PER 5ML SYRUP
This is what my request url looks like:
http://localhost:4200/api/search?key=VITAMINS%20+%20ZINC%20100MG/10MG%20PER%205ML%20SYRUP
Here is my express router looks like:
router.get('/search', (req, res, next) => {
const query = req.query;
console.log(query.key);
.... omitted
});
The problem here is i can't retain the +
special character and thus end up getting this log printed which is not the original criteria inside the router.
VITAMINS ZINC 100MG/10MG PER 5ML SYRUP
Upvotes: 0
Views: 1446
Reputation: 1510
In terms of URI, +
character is one of the reserved characters.
When a character from the reserved set (a "reserved character") has special meaning (a "reserved purpose") in a certain context, and a URI scheme says that it is necessary to use that character for some other purpose, then the character must be percent-encoded.
In your case, query string needs to be percent-encoded before hitting the URL at client side :
encodeURIComponent("VITAMIN + ZINC")
And then at server side (i.e. express) decode the query using decodeURIComponent()
.
Upvotes: 2