HttpClient Utility
HTTP client utility
Use this utility to make an HTTP-callout.
Example of usage:
let header = new Headers();
header.addHeader("Content-Type", "application/json");
header.addHeader("Accept-Language", "ja");
let payload = new Parameters();
payload.addParameter("scope", ["openid", "profile", "email"]);
payload.addParameter("intent-id", "5298SFFLS735L");
// use Parameters to construct payload
let rsp = HttpClientV2.httpPost("https://www.externalconsent.org", headers, payload, "rt_profile", null, null, null, null, null, null, 5, true, null);
if (rsp.hasError()) {
// handle error...
} else {
// process response
}
// use string as payload
let rsp2 = HttpClientV2.httpPost("https://www.externalconsent.org", headers, "string payload", "rt_profile", null, null, null, null, null, null, 5, false, null);
if (rsp.hasError()) {
// handle error...
} else {
// process response
}
1. HttpClientV2
Return Type | Method | Description |
---|---|---|
HttpResponse | httpGet(url, headers, httpsTrustStore, basicAuthUsername, basicAuthPassword, clientKeyStore, clientKeyAlias, protocol, throwExec, timeout, proxyServer) | Send HTTP GET request to the specified URL. |
HttpResponse | httpPut(url, headers, paramsOrPayload, httpsTrustStore, basicAuthUsername, basicAuthPassword, clientKeyStore, clientKeyAlias, protocol, throwExec, timeout, sendDataAsJson, proxyServer) | Send HTTP PUT request to the specified URL. |
HttpResponse | httpPost(url, headers, paramsOrPayload, httpsTrustStore, basicAuthUsername, basicAuthPassword, clientKeyStore, clientKeyAlias, protocol, throwExec, timeout, sendDataAsJson, proxyServer) | Send HTTP POST request to the specified URL. |
HttpResponse | httpPatch(url, headers, paramsOrPayload, httpsTrustStore, basicAuthUsername, basicAuthPassword, clientKeyStore, clientKeyAlias, protocol, throwExec, timeout, sendDataAsJson, proxyServer) | Send HTTP PATCH request to the specified URL. |
HttpResponse | httpDelete(url, headers, httpsTrustStore, basicAuthUsername, basicAuthPassword, clientKeyStore, clientKeyAlias, protocol, throwExec, timeout, proxyServer) | Send HTTP DELETE request to the specified URL. |
The following table describes the method of arguments:
Name | Data type | Required | Description |
---|---|---|---|
url | string | Yes | Valid HTTP URL. |
headers | Headers | No | Extra HTTP headers. |
paramsOrPayload | Parameters OR string | No | Parameters to be added to the HTTP request body. Alternatively, use raw string as the HTTP request body. |
httpsTrustStore | string | No | The truststore to use. If an HTTPS connection is needed and this method is set to null , the default truststore that is specified in the provider configs is used. |
basicAuthUsername | string | No | Basic authorization header username. |
basicAuthPassword | string | No | Basic authorization header password. |
clientKeyStore | string | No | Client keystore. If null , client certificate authorization is disabled. |
clientKeyAlias | string | No | Client key alias. If null , client certificate authorization is disabled. |
protocol | string | No | Not used. This method is a mockup parameter for Verify Access compatibility. |
throwExec | Boolean | No | Not used. This method is a mockup parameter for Verify Access compatibility. |
timeout | int | No | Request timeout in seconds. A value of 0 results in a no connection timeout. If set to a value that is less than 0, the timeout is set to 5 seconds. |
sendDataAsJson | Boolean | No | Specifies whether the parameters are in JSON. Defaults to false if not provided. |
proxyServer | string | No | The full name of the proxy server to use. For example, https://proxy.com:443. Set to null if a proxy server is not needed. |
2. HttpResponse
This object represents the HTTP response received.
Return Type | Method | Description | Arguments |
---|---|---|---|
string | getBody() | Get the HTTP response body. | |
integer | getCode() | Get the HTTP response state code. | |
JS object | getHeaders() | Get the HTTP response headers. | |
string[] | getHeaderKeys() | Get the HTTP response header names. | |
string[] | getHeaderValues(name) | Get the HTTP response header value of the specified name. | string |
Boolean | hasError() | Checks whether any errors exist. This return type is called first to check whether an error exists when a new HttpResponse object is return from an HttpClient method. | |
string | getError() | Retrieve the error returned. |
3. Headers
This class represents the HTTP header to be used in the HTTP request.
Return Type | Method | Description | Arguments |
---|---|---|---|
Headers() | Constructor of Headers class | ||
addHeader(name, value) | Add a header | string, string | |
addHeader(name, values) | Add a header | string, string[] | |
string[] | getHeader(name) | Get the header values with the specified name. Returns null if no such header name. | string |
string[] | getHeaderNames() | Get all the header names. Returns null if no headers exist. | |
JS object | getHeaders() | Get all the headers |
4. Parameters
This class stores the parameters to be added to the HTTP request body.
Return Type | Method | Description | Arguments |
---|---|---|---|
Parameters() | Constructor of Parameters class | ||
addParameter(name, value) | Add a parameter | string, string | |
addParameter(name, values) | Add a parameter | string, string[] | |
string[] | getParameter(name) | Get the parameter values with the specified name. Returns null if no such parameter name. | string |
string[] | getParameterNames() | Get all the parameter names. Returns null if no parameters exist. | |
JS object | getParameters() | Get all the parameters |
Note: If HttpClient method sendDataAsJson
parameter is set to true
, the parameters are treated as JSON data.
Otherwise, it is treated as form data. Ensure that the HTTP header Content-Type
is set to the wanted value.
Updated about 2 years ago