kishore
kishore

Reputation: 1748

How to send integers in query parameters in nodejs Express service

I have a nodejs express web server running on my box. I want to send a get request along with query parameters. Is there any way to find type of each query parameter like int,bool,string. The query parameters key value is not know to me. I interpret as a json object of key value pairs at server side.

Upvotes: 35

Views: 74170

Answers (8)

Akshay Vijay Jain
Akshay Vijay Jain

Reputation: 15955

Actually we can send number, it will be sent as string=>solution is to parse it at the proper time
I use zod library to parse incoming request body
so I use

const parser = z.coerce.number().min(1).max(20).optional();
parser.parse(data)

Thus incoming string is parsed into number;

Upvotes: 0

wael32gh
wael32gh

Reputation: 459

This have been answered a long ago but there's a workaround for parsing query params as strings to their proper types using 3rd party library express-query-parser

// without this parser
req.query = {a: 'null', b: 'true', c: {d: 'false', e: '3.14'}}

// with this parser
req.query = {a: null, b: true, c: {d: false, e: 3.14}}

Upvotes: 2

Anil Jat
Anil Jat

Reputation: 13

//You can use like that

let { page_number } : any = req.query;

    page_number = +page_number;

Upvotes: -1

Christopher Nolan
Christopher Nolan

Reputation: 1107

let originalType = JSON.parse(req.query.someproperty);

In HTTP, querystring parameters are treated as string whether you have originally sent say [0,1] or 5 or "hello". So we have to parse it using json parsing.

Upvotes: 1

hierd
hierd

Reputation: 1

Maybe this will be of any help to those who read this, but I like to use arrow functions to keep my code clean. Since all I do is change one variable it should only take one line of code:

module.exports = function(repo){
    
  router.route('/:id',
  (req, res, next) => { req.params.id = parseInt(req.params.id); next(); })
    .get(repo.getById)
    .delete(repo.deleteById)
    .put(repo.updateById);
}

Upvotes: -2

parvezp
parvezp

Reputation: 191

You can also try

var someProperty = (+req.query.someProperty);

This worked for me!

Upvotes: 14

Rori Stumpf
Rori Stumpf

Reputation: 1977

As mentioned by Paul Mougel, http query and path variables are strings. However, these can be intercepted and modified before being handled. I do it like this:

var convertMembershipTypeToInt = function (req, res, next) {
  req.params.membershipType = parseInt(req.params.membershipType);
  next();
};

before:

router.get('/api/:membershipType(\\d+)/', api.membershipType);

after:

router.get('/api/:membershipType(\\d+)/', convertMembershipTypeToInt, api.membershipType);

In this case, req.params.membershipType is converted from a string to an integer. Note the regex to ensure that only integers are passed to the converter.

Upvotes: 11

Paul Mougel
Paul Mougel

Reputation: 17038

You can't, as HTTP has no notion of types: everything is a string, including querystring parameters.

What you'll need to do is to use the req.query object and manually transform the strings into integers using parseInt():

req.query.someProperty = parseInt(req.query.someProperty);

Upvotes: 47

Related Questions