Updated With More Elegant Code: Invoking Uber REST API From Server Code

Screen Shot 2015-10-14 at 2.50.55 PM
Invoking Uber API

Last Friday I blogged how to invoke Uber REST API from the server. I was looking at Uber documentation and realized there is a better and more elegant way to invoke the API. Uber specifically provides a server token. Instead of using a header parameter, I have updated the script to use the server token. This is how the code looks now:

// service input
var lat = request.get("lat");
var lng = request.get("lng");
var url = "https://api.uber.com/v1/products";
var token = "4dPYezRBsWmqpV_XOyhIb2lndGsRfJaYhhXvJBRY";

var parameters = {
  "server_token": token,
  "latitude": lat,
  "longitude": lng

// send Ajax request
var res = XHR2.send("GET", url, {
// response
response.success(res, "application/json");

Also, instead of attaching the parameters to the URL, the script creates parameters object with all the inputs for the API. This script is more elegant and simpler to understand.

1 Comment

  1. Virat Tripathi says:

    Script uberproducts: :16:0 Expected : but found } }); ^

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.