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.

1 2 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:

1 2 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

Option

Type

Description

url

String

The absolute (full) request URL

method

String

The request method to use. GET is default

type

String

Alias for method

username

String

Username of the user to perform the request as

timeout

String or Integer

Connection timeout in milliseconds

data

String

JSON data for request body. Appended to URL for GET requests. Use JSON.stringify(data) to convert objects to JSON string.

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.

1 2 3 4 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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 var HashMap = Java.type("java.util.HashMap"); var config = new HashMap(); config.put("method", "POST"); config.put("url", baseUrl + "/rest/api/2/search"); var data = { "jql": "project = JSDDEFAULT", "startAt": 0, "maxResults": 1 }; config.put("data", JSON.stringify(data)); var json = jemhUtils.request(config); if (json.has("issues")) { var key = json.get("issues").get(0).get("key"); resultMap.put("issueKey", key); } else if (json.has("message")) { print(json.get("message")); }

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 message

  • stackTrace - the exception stack trace

  • cause - the cause of the exception (if present)

1 2 3 4 5 6 7 8 9 10 11 12 13 var HashMap = Java.type("java.util.HashMap"); var config = new HashMap(); config.put("url", "https://api.github.com/search/users"); var data = { q: "octocat"}; config.put("data", JSON.stringify(data)); var json = jemhUtils.request(config); if (json.has("items")) { var url = json.get("items").get(0).get("html_url"); resultMap.put("customfield_10400", url); } else if (json.has("message")) { print(json.get("message")); }