Request Wrapper
Request Wrapper provides a way to fetch data from REST APIs. For users using the API, it usually involves making HTTP requests to the service and getting a response from it. The global HTTP client accepts a Request object as an argument and returns the Response object.
Request
The Request interface represents a resource request. You can create a new Request object using the Request() constructor. There are two ways to get the data-
- Fetching data from URL- It's a Uniform Resource Locator that type is String used to get the data from the URL
Example-
const request = new Request('https://www.mozilla.org/favicon.ico');
- Getting data from options -
Options | Type | Description |
---|---|---|
Headers | Dictionary | Headers are used by server to process request. |
Method | String | REST API uses HTTP methods (Get, PUT, POST) to do CRUD options on a resource (Create, Read, Update and Delete) |
Content_type | String | Content_type refers to the type of content that server should expect in order to properly accept the request. |
Request_body | String | A data payload is sent to the sever in the request body. You can pass the parameters in request body as well. |
Example-
const request = new Request('https://example.com', {method: 'POST', request_body: '{"foo": "bar"}'});
HTTP
To fetch the data from the requested resource, use the Global HTTP Client. It prevents you from fetching requests larger than 5 mb. Additionally, the URLs from which you are attempting to fetch the data should be whitelisted by the product.
## Returns the Response object
https.fetch(resource)
## Returns the Response in text format
Note
Please connect via Slack to have your URLs added to the Whitelist
Response
The response interface represents a response to a request. The data that sent back to you after making a request to a server is known as response. Response Interface contains the following properties-
Properties | Description |
---|---|
Body | The body of the response. Set to a string. |
Headers | A Response header acting as dictionary contains the additional information about the page content. |
Code | The Http status code for the result. |
Request | It's a URL from which the data has been fetched. |
Example-
function process(rows, context) {
var request = new Request("https://jsonplaceholder.typicode.com/todos/2");
var response = https.fetch(request);
script.log("Headers",response.headers)
script.log("Code",response.code)
script.log("Body",response.body.string())
script.log("Body",response.request)
return rows;
}
Advanced Options
Http also enables you to querying the native ohkttp request object, for example-
var Request = Java.type("okhttp3.Request")
var MediaType = Java.type("okhttp3.MediaType")
var RequestBody = Java.type("okhttp3.RequestBody")
var Response = Java.type("okhttp3.Response")
public static final MediaType JSON
= MediaType.get("application/json; charset=utf-8");
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(json, JSON);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try (Response response = https.fetch(request)) {
return response.body().string();
}
}