pjs.sendRequest()

pjs.sendRequest()

The pjs.sendRequest() API is deprecated. Use pjs.httpRequest() for new development.

Description

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

Parameters

  1. 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.

  2. String URL - The endpoint to your web service.

  3. 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 could not 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.

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 greater than or equal to 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 the response code is greater than or equal to 400. To read the body even when the response code is greater than or equal to 400, supply the alwaysReadBody property in the argument object.

alwaysReadBody is available in Profound.js version 5.8.6 and later.

Example

Example of General Use

// Simple form: request type, URL, and body. Passing an object as the body // sends JSON and parses the JSON response back into an object. var result = pjs.sendRequest("post", "https://website.com/myJSONAPI", {key: 'value'}); // Advanced form: a single options object, passed through to the request module. result = pjs.sendRequest({ method: "POST", uri: "https://website.com/myJSONAPI", body: {key: 'value'}, json: true, timeout: 5000, time: true });