1

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?

8
  • For your first question, have a look at JQuery.param Commented Jul 27, 2014 at 19:36
  • And for your second question, if you use JQuery.param, then you can just access the coordinates using req.query.coordinates[x][y] in the route in Express. Commented Jul 27, 2014 at 19:48
  • Why not use the query string? That's what it's there for. You can either use the PHP/Rails/jQuery-style query string parameters with arrays as already mentioned, or you could just encode everything as JSON, encode it properly for the query string, and assign to a variable like data or something. There are lots of ways to handle this, but the last thing I would do in this case is attach it to part of the path. Commented Jul 27, 2014 at 20:55
  • No - see my second comment. You should be using req.query not req.params. Commented Jul 27, 2014 at 21:33
  • Thanks @mccannf but req.query.coordinates returns undefined. Commented Jul 27, 2014 at 21:49

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.