Bixby Developer Center

Guides

http

Provides helper methods for HTTP requests.

Note

All calls in the HTTP module are made synchronously and will wait until a system-defined timeout. It is usually not appropriate to wrap HTTP calls from Bixby in promises or callbacks. All methods accept both HTTP and HTTPS (secure) URLs.

var http = require('http');

HTTP Options

The following HTTP methods all take an options argument:

The options argument is a JSON object that can contain the following parameters:

  • format: This specifies how the response will be parsed.
    • text: returns the raw response text, with no parsing.
    • json: expects the response to be JSON. Returns a JavaScript object.
    • xmljs: expects the response to be XML. The XML is converted to JSON, and then returned as a JavaScript object.
    • csv: expects the response to be a comma-separated value list. This will return an array of string values, or null if the CSV is not well-formed.
    • url: expects the response to be URL-encoded key/value pairs. Decodes the response and returns a JavaScript object.
  • query: An object with attribute/value pairs to pass as arguments to the REST service.
  • basicAuth: HTTP Basic Authentication. The value must be an object with "username" and "password".
  • cacheTime: Cache time in milliseconds. All requests are cached in memory for cacheTime, which defaults to 3600000 (1 hour). An alternate cache time for a specific request can be provided. Set cacheTime to 0 to disable caching for requests. This can be particularly useful for POST requests.
  • headers: An object with key/value pairs for additional HTTP headers. This may be used for custom Content-Type settings (such as XML).
  • passAsJson (POST call only): If set to true, passes the request parameters in the body in JSON format and sets the content type appropriately.
  • returnHeaders: If set to true, returns HTTP headers and status code. This also suppresses exceptions on HTTP error status codes, leaving this to the developer to handle.

Example

// Set options: the response is expected in JSON, and the query is a
// JSON object of { "searchTerm": "value of searchTerm variable" }

var options = {
format: 'json',
query: {
searchTerm: searchTerm
}
};

// call endpoint defined in remote.url configuration, parse the JSON
// response, return resulting object

var response = http.getUrl(config.get('remote.url') + '/search', options);
Note

If you use returnHeaders with a POST request, the entire response from the API will be wrapped in an additional JSON object:

{
"status": 200,
"headers": {
"header1": "value"
},
"responseText": "text form of response",
"parsed": {
"key1": "value1",
"key2": "value2"
}
}

The parsed key will contain the parsed data returned from the API, as specified by the format option.

Setting HTTP Headers

You can set headers information in the options parameter, like in the following example:

let options = {
format: 'json',
headers: {
'accept': 'application/json'
},
};

let response = http.getUrl('https://my-capsule.com/api/search/', options);

Cache Settings

Result caching is affected by either of the following:

  • The cacheTime option in an HTTP request (see HTTP Options).
  • The Cache-Control HTTP header in the HTTP response sent by the server.

Bixby supports the following Cache-Control settings:

  • no-cache or no-store: The result will not be cached at all, regardless of the cacheTime setting.
  • max-age=<seconds>: The result will be cached for a maximum of the specified time.

If both cacheTime and Cache-Control: max-age are specified for the same request, the result will be cached for the shorter of the two values.

http.getUrl(url, options)

Perform an HTTP GET request. By default, the return value is a string (equivalent to setting the format option to text); this can be changed by using a different format option in the options argument.

Kind: Static method of http
Access: Public

ParamTypeDescription
urlString
optionsObjectSee HTTP options

For an example of using http.getUrl(), read Calling a Web Service from a Local Endpoint.

http.postUrl(url, params, options)

Perform an HTTP POST request.

Kind: Static method of http

Supports all the options from getUrl(), plus the following:

  • passAsJson: If true, pass the request parameters in the body in JSON format.
ParamTypeDescription
urlString
paramsObjectUnencoded keys and values for the POST body
optionsObjectSee HTTP options

http.deleteUrl(url, params, options)

Perform an HTTP DELETE request.

Kind: Static method of http

Supports all the options from getUrl(), plus the following:

  • passAsJson: If true, pass the request parameters in the body in JSON format
ParamTypeDescription
urlString
paramsObjectUnencoded keys and values for the DELETE body
optionsObjectSee HTTP options

http.putUrl(url, params, options)

Perform an HTTP PUT request.

Kind: Static method of http

Supports all the options from getUrl(), plus the following:

  • passAsJson: If true, pass the request parameters in the body in JSON format
ParamTypeDescription
urlString
paramsObjectUnencoded keys and values for the POST body
optionsObjectSee HTTP options

http.oauthGetUrl(url, options)

Perform an HTTP GET request, attached with the OAuth appropriate access keys. (See Implementing JavaScript Actions for details.)

Kind: Static method of http
Access: Public

Supports the same options as getUrl().

ParamTypeDescription
urlString
optionsObjectSee HTTP options

http.oauthPostUrl(url, params, options)

Perform an HTTP POST request, attached with the OAuth appropriate access keys. (See Implementing JavaScript Actions for details.)

Kind: Static method of http
Access: Public

Supports the same parameters and options as postUrl().

ParamTypeDescription
urlString
paramsObjectUnencoded keys and values for the POST body
optionsObjectSee HTTP options

http.oauthDeleteUrl(url, params, options)

Perform an HTTP DELETE request, attached with the OAuth appropriate access keys. (See Implementing JavaScript Actions for details.)

Kind: Static method of http
Access: Public

ParamTypeDescription
urlString
paramsObjectUnencoded keys and values for the DELETE body
optionsObjectSee HTTP options

Supports the same parameters and options as deleteUrl().

http.oauthPutUrl(url, params, options)

Perform an HTTP PUT request, attached with the OAuth appropriate access keys. (See Implementing JavaScript Actions for details.)

Kind: Static method of http
Access: Public

Supports the same parameters and options as putUrl().

ParamTypeDescription
urlString
paramsObjectUnencoded keys and values for the PUT body
optionsObjectSee HTTP options

http.spsProxyPost(options)

Delivers an SPS payment via an HTTP POST request. This method allows you to use specific payment-related variables and functions when sending the request body. For more information, see the Supporting Payments topic.

Kind: Static method of http

ParamTypeDescription
optionsObjectSee HTTP options

In the capsule's local JavaScript, you should define the options parameter like the following:

var options = {
headers: {
"Content-Type": "application/json",
"request_num": 1
},
body: {
"card_number": "{{credit_card_number}}",
"card_cvv": "{{credit_card_verification_value}}",
"card_expire_month": "{{credit_card_month}}",
"card_expire_year": "{{credit_card_year}}"
},
url: "https//example.com/post"
};
// url can also be specified with `url` key in `http-delivery` block
var response = https.spsProxyPost(options);

http.spsProxyPut(options)

Delivers an SPS payment via an HTTP PUT request. This method allows you to use specific payment-related variables and functions when sending the request body. For more information, see the Supporting Payments topic.

Kind: Static method of http

Supports the same parameters and options as http.spsProxyPost.

ParamTypeDescription
optionsObjectSee HTTP options

http.makeQueryString(args)

Return a properly-formatted query string.

Kind: Static method of http
Access: Public

ParamType
argsObject

The object key-value pairs are used as the query parameter keys and values. Values will be URL-encoded. You can pass this object to http.makeQueryString():

{"color": "red", "flavor": "peppermint stick"}

The queryString returned is:

color=red&flavor=peppermint%20stick