Node.js Express REST API - accessing JSON url params with GET request

I have a REST endpoint that is /geo/search and requires a number of long/lat coordinates to be sent as part of the request (GEO polygon).

  • Is there any way I can use JSON in the GET request? I was thinking that that URL encoding may be a solution:

    var data = encodeURIComponent({"coordinates":[[-122.610168,37.598167],[-122.288818,37.598167],[-122.288818,37.845833],[-122.610168,37.845833],[-122.610168,37.598167]]});
    
  • How would I access these params in the route?

Answer to #1 - thanks to @mccannf

Using JQuery.param:

Client:

var test = {"coordinates":[[-122.610168,37.598167],[-122.288818,37.598167],[-122.288818,37.845833],[-122.610168,37.845833],[-122.610168,37.598167]]};

    console.log($.param( test ));

Outputs:

coordinates%5B0%5D%5B%5D=-122.610168&coordinates%5B0%5D%5B%5D=37.598167&coordinates%5B1%5D%5B%5D=-122.288818&coordinates%5B1%5D%5B%5D=37.598167&coordinates%5B2%5D%5B%5D=-122.288818&coordinates%5B2%5D%5B%5D=37.845833&coordinates%5B3%5D%5B%5D=-122.610168&coordinates%5B3%5D%5B%5D=37.845833&coordinates%5B4%5D%5B%5D=-122.610168&coordinates%5B4%5D%5B%5D=37.598167 

Answer to #2 - thanks to @Brad:

Server - Express route:

router.get('/search_polygon', function(req, res) {
    console.log('Server received: ' + JSON.stringify(req.query.coordinates));
...

Outputs:

Server received: [["-122.610168","37.598167"],["-122.288818","37.598167"],["-122.288818","37.845833"],["-122.610168","37.845833"],["-122.610168","37.598167"]]

My issue was trying to pass these as part of the path, and not as parameters as they should be.