Performing HTTP requests
HTTP requests can be made using the utility class provided in order to access internal or external REST APIs.
The utility class can be accessed in JEMH’s JavaScript contexts via jemhUtils
.
Basic
The simplest way to call a local (to Jira) REST API is to use invokeLocalRest
. Note that here the path given is relative to the Jira base URL. This will create an authenticated TrustedRequest.
var json = jemhUtils.invokeLocalRest("/rest/api/latest/issue/TEST-1");
print(json); //prints to the log and visible during config previewTO
To invoke an unauthenticated request, use invokeLocalAnonRest
:
var json = jemhUtils.invokeLocalAnonRest("/rest/api/latest/issue/TEST-1");
print(json);
Advanced
since 3.1.5
For more control over the parameters of the HTTP request, use the request
method. It takes a Map<String, Object>
of configuration options.
Options
Option | Type | Description |
---|---|---|
|
| The absolute (full) request URL |
|
| The request method to use. GET is default |
|
| Alias for |
|
| Username of the user to perform the request as |
|
| Connection timeout in milliseconds |
|
| JSON data for request body. Appended to URL for GET requests. Use |
GET example
The below shows the minimum required in order to perform a GET request. Note that query parameters can also be added using the data
option as per the POST example in the next section.
var HashMap = Java.type("java.util.HashMap");
var config = new HashMap();
config.put("url", "https://api.github.com/search/users?q=octocat");
var json = jemhUtils.request(config);
POST example
The below shows how data can be sent in a HTTP request using the data
option.
Handling the returned response
The request
method returns the response in a JSONObject. If a response is received, the JSON data returned will reflect this.
If an exception occurs server-side during the request process, the returned JSON will have the following nodes:
message
- the exception messagestackTrace
- the exception stack tracecause
- the cause of the exception (if present)