httpClient
Overview¶
Used when using an HTTP client to connect to an external service in a "Server Script".
Properties¶
| No | Name | Get | Set | Type | Description |
|---|---|---|---|---|---|
| 1 | RequestUri | Yes | Yes | string | The URI of the destination |
| 2 | Content | Yes | Yes | string | Data to send |
| 3 | Encoding | Yes | Yes | string | Encoding (default is "utf-8") |
| 4 | MediaType | Yes | Yes | string | Media type (default is "application/json") |
| 5 | RequestHeaders | Yes | No | Dictionary | Request header. See below for more details. |
| 6 | ResponseHeaders | Yes | No | Dictionary | Response header. See below for more details. |
| 7 | IsSuccess | Yes | No | bool | Get whether the most recent request was successful. Returns True if the StatusCode is within the range of 200-299. |
| 8 | StatusCode | Yes | No | int | Status code of the response message for the most recent request. |
| 9 | TimeOut | Yes | Yes | int | Specify the timeout of the HTTP request in milliseconds. It can be set within the range from "ServerScriptHttpClientTimeOutMin" to "ServerScriptHttpClientTimeOutMax" in Script.json. (The default value is the value set in "ServerScriptHttpClientTimeOut" in Script.json.) |
| 10 | IsTimeOut | Yes | No | bool | Get whether the most recent request was aborted by a timeout. Returns True if it timed out. |
Example Usage of RequestHeaders Property¶
You can add any request header by using the Add method of the RequestHeaders property as shown below.
Basic Authentication with Authorization Header¶
let base64 = utilities.ConvertToBase64String('userName:password');
httpClient.RequestHeaders.Add("Authorization", "Basic " + base64);
httpClient.RequestUri= "https://servername/api/.....";
let result = httpClient.Get();
*To convert a string to Base64, you can use the "utilities.ConvertToBase64String" method.
Also, if you want to set another header and send it subsequently, reset the header content with the Clear method.
httpClient.RequestHeaders.Clear();
httpClient.RequestHeaders.Add("Authorization", "Bearer " + "X2kKRHGI495Y.......");
Example Usage of ResponseHeaders Property¶
When accessing an external API as shown below, you can get the value of any response header property by using the Item.get method of the ResponseHeaders property.
Getting the Value of the Access-Control-Allow-Origin Property from the Response Header¶
// Connect to the external API
httpClient.RequestUri="https://savername/api/......";
let response = httpClient.Get();
// Get the response header
var value = httpClient.ResponseHeaders.Item.get("Access-Control-Allow-Origin");
context.Log(value[0]);
Also, if you want to send another request subsequently and get the property value of the response header, reset the header content with the Clear method beforehand.
httpClient.RequestUri="https://savername/api/....../123/...";
let response = httpClient.Get();
var value = httpClient.ResponseHeaders.Item.get("Access-Control-Allow-Origin");
context.Log(value[0]);
// Clear ResponseHeaders before sending the next request
httpClient.ResponseHeaders.Clear();
httpClient.RequestUri="https://savername/api/....../456/...";
response = httpClient.Get();
Example Usage of TimeOut Property¶
In the following example, you can change the timeout period of the request by setting a value to the TimeOut property before sending the request.
httpClient.RequestUri= "https://servername/api/.....";
httpClient.TimeOut = 300000;
let result = httpClient.Get();
Example Usage of IsTimeOut Property¶
In the following example, if the request fails, it determines whether the reason for the failure was a timeout, and if so, executes httpClient.Get again.
let response = httpClient.Get();
if (!httpClient.IsSuccess) { // The request failed
if (httpClient.IsTimeOut) { // It was interrupted by a timeout
response = httpClient.Get(); // Retry
}
}
Methods¶
| No | Name | Description |
|---|---|---|
| 1 | Get | Issue the GET method and receive the results. |
| 2 | Post | Issue the POST method and receive the results. |
| 3 | Put | Issue the PUT method and receive the results. |
| 4 | Delete | Issue the DELETE method and receive the results. |
| 5 | Patch | Issue the PATCH method and receive the results. |
Supported Versions¶
RequestHeaders¶
Pleasanter 1.3.9.0 or later
HttpClient¶
Pleasanter 1.2.16.0 or later
HttpClient Patch Method¶
Pleasanter 1.3.50.0 or later
Related Information¶
- Manage Table: Server Script
- Developer's Guide: Server Script: utilities.ConvertToBase64String
- Developer's Guide: Server Script: httpClient.Get
- Developer's Guide: Server Script: httpClient.Post
- Developer's Guide: Server Script: httpClient.Put
- Developer's Guide: Server Script: httpClient.Delete
- Developer's Guide: Server Script: httpClient.Patch