pjs.sendRequest()

pjs.sendRequest is deprecated. Please use pjs.httpRequest() for new development.

API Overview

The pjs.sendRequest() API is used to send requests to web services.

Parameters

  • Object options/String requestType
    If you pass a string, you can pass the request type (get, post). If you pass an object, it will be passed into the request module API (see module API options). If an object is passed, you do not need to pass any other parameters.  You may also set alwaysReadBody: true in this object to allow documents (such as JSON documents) to be received even when the HTTP status code would normally indicate an error.
  • String URL
    The endpoint to your web service.
  • String|Object Body (optional)
    The body of the request. If you pass in an object, it will send the request in JSON and it will also return JSON. Otherwise, it will pass as text and return a String.

Return Value

Object / String The response body.



Exception Handling

This API will throw an exception if the HTTP request couldn't be completed (such as for a networking problem), or if the HTTP request completes with a status code of 400 or higher.  Starting with Profound.js 5.8.6, you may prevent throwing errors for status codes 400 or higher by setting the alwaysReadBody option (see above.)

Examples

Example of General Use
//Simple
var result = pjs.sendRequest("post", "http://website.com/myJSONAPI", {key: 'value'});
 
//Advanced, using request API options
result = pjs.sendRequest({
      method: "POST",
      uri: "http://website.com/myJSONAPI",
      body: {key: 'value'},
      json: true,
	  timeout: 5000,
      time: true
});

Always Read Body

The default behavior of pjs.sendRequest is to interpret the response as an error if the remote API returns an HTTP status code >= 400, in which case response body data is not passed to the Profound.js program. Instead, an error is thrown.

Some APIs return useful information back in the response body even when then response code is >= 400. To read the body even when the response code is >= 400, supply the "alwaysReadBody" property to the argument object.

"alwaysReadBody" is available in PJS version 5.8.6 and later.