35

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.

1
  • 2
    you could send a json object in the get request like http://domain.com?json={"key":intValue} and then parse the value of query param json into a JSON object with JSON.parse(req.json) Commented Mar 22, 2014 at 20:02

8 Answers 8

47

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);
Sign up to request clarification or add additional context in comments.

2 Comments

is there any other way like custom headers or something where i can send the query parameters as json object.
We ran into similar requirement where JSON needs to be sent in a get request. We ended up using POST instead... Yes, POST sounds odd for doing a GET but that's one way to get around it.
14

You can also try

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

This worked for me!

Comments

11

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.

2 Comments

I think this solution throws a typescript error since req.params is defined as a map: [key: string]: string; and thus you can't assign an integer to the string value. I was really excited about it though as it would be reusable for every get endpoint that accepts an id as a path param
With TypeScript you don't get an error if you declare your request parameter as req: Request<{}, {}, {}, { id: number }> inside your function.
2

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}}

Comments

1
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.

Comments

0

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;

Comments

-1

//You can use like that

let { page_number } : any = req.query;

    page_number = +page_number;

1 Comment

A good answer will always include an explanation why this would solve the issue, so that the OP and any future readers can learn from it.
-2

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);
}

1 Comment

Be careful not to "clean" your code at the expense of readability.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.