Toggle menu

request

This module performs http requests.

This library is distributed under the Apache 2.0 license, its source and full documentation can be found at www.npmjs.com/package/request

Note that although shipped with the platform, this library is deprecated. Use axios instead.

Executed

Server-side.

Details

var request = require("request");

This module is asynchronous. Depending upon how it is used, it may be necessary to use the synchronise module so that it behaves in a synchronous manner. Request also provides support for file streaming, multi-part form data, HTTP authentication, custom HTTP headers, OAuth signing, proxies, UNIX domain sockets, TLS/SSL and follows redirects by default.

Timeouts
You must set a timeout (in milliseconds) when using the request module. Failing to set a timeout on a request that fails to get a response could potentially tie up API Server resources until manually restarted. Timeouts are set in the options object, see below for an example and www.npmjs.com/package/request#timeouts for more information.

Examples

These first three examples all use request's convenience methods to make simple GET and POST requests.

Simple POST

This will post the string "text2" to the URL specified.

function( params, credentials ) {
    var request = require("request");
    request.post("http://requestb.in/1np8mbh1", {
        "body": "text2",
        "timeout": 5000
    });
}

JSON POST

This will post a JSON object to the URL specified. Note we need to include json:true. This sets the body to JSON and adds the Content-type: application/json header. It also automatically parses the response body as JSON.

function( params, credentials ) {
    var request = require("request");
    request.post("http://requestb.in/1np8mbh1", {
        body:{
            "name":"value"
        },
        "timeout": 5000,
        "json": true
    });
}

GET Request with Synchronise

If the get wasn't wrapped in sync.await and sync.defer of the synchronise library the End Point will have returned before the asynchronous operation completes, so you'd never see the response.

Note how request() defaults to request.get()

function(params, credentials) {  
    var request = require("request");  
    var sync = require("synchronize");   
    var result  = sync.await(request("http://httpbin.org/get", {
        "timeout": 5000
    }, sync.defer()));   
    return result;
}

Supplying Additional Options

In most real-world cases you'll need to do more than make simple GET and POST requests. You can include things like custom headers, the request body, form data, and authorisation details in the options object. See the request options section of the NPM documentation for a complete list. Note how the method also has to be set in the object.

function(params, credentials) {
    var request = require('request');
    var sync = require('synchronize');
    var body = {
        "name": "Tim",
        "job": "Tech Author"
    };
    var options = {
        "url": "https://949f1d54-****-****-2c62598fe8ff.mock.pstmn.io/securemockpost",
        "headers": {
            "x-api-key": "863********79ffb"
        },
        "body": body,
        "json": true,
        "method": "POST",
        "timeout": 5000
    };
    var result = sync.await(request(options, sync.defer()));
    return result;
}

Posting URL Encoded Forms

Similar to the example above, it's possible to POST form data. See the forms section of the library docs for more examples.

function(params, credentials) {
    var request = require("request");
    var sync = require("synchronize");
    var options = {
        "url": "https://www.mygreatservice.com/rest/",
        "form": {
            "f": "json",
            "client_id": "YOUR_APPLICATIONS_CLIENT_ID",
            "client_secret": "YOUR_APPLICATIONS_CLIENT_SECRET",
            "grant_type": "client_credentials",
            "expiration": "1440"
        },
        "timeout": 5000,
        "json": true,
        "method": "POST"
    };
    var result = sync.await(request(options, sync.defer()));
    return result;
}

This results in a posted value of:

f=json&client_id=YOUR_APPLICATIONS_CLIENT_ID&client_secret=YOUR_APPLICATIONS_CLIENT_SECRET&grant_type=client_credentials&expiration=1440

Last modified on October 11, 2022

Share this page

Facebook icon Twitter icon email icon

Print

print icon