< Previous Article Next Article >

pjs.sendRequest()

The pjs.sendRequest() API consumes a RESTful web service using server-side code and returns the web service response.

The API accepts a configuration parameter with the following properties:

  • method - the HTTP method to use (POST, GET, PUT, etc.)
  • uri - the relative or absolute URI for the request
  • body - request body or post data to pass along to the web service

POST example using a relative URI:

let response = pjs.sendRequest({
  method: "POST",
  uri: "/updatePrice"
  body: {
    price,
    effectiveDate: new Date()  // today
  }
});

GET example using an absoulte URI:

// Get exchange rate data
let response = pjs.sendRequest({
  method: "GET",
  uri: "https://api.exchangeratesapi.io/latest?base=" + baseCurrency
});

// Load Rich Display grid with exchange rates
for (let currency in response.rates) {
  display.grid.push({
    currency,
    rate: response.rates[currency]
  })
}

Questions?

Have questions about this topic? Ask for help on our NodeRun Discussion Forum.

< Previous Article Next Article >