How to access the attributes of an http response in another http task?

Hey.

The way I do this is: I always put a Script task before an Http task. In that Script task, I set up everything I will need in the REST API call done from Http task.

Example of a Groovy script that sets up my request body, headers and endpoint URL:

final String json = '{"idDokumenta":@docid@,azuriraj":false}';
final String dokumentid = execution.getVariable("dokumentid").toString();
String res = json.replace("@docid@", dokumentid);
execution.setVariable("requestBody", res);
execution.setVariable("requestUrl", "http://localhost:8080/api/test");
execution.setVariable("requestHeaders", "Accept: application/json");

Then, in the Http task configuration/properties, you do/fill this:

Little below that, you need to do fill stuff like this as well:

After the HTTP call has been made, your response JSON will be saved in the process variable under the name myResponse.

You can use that variable in the future Script tasks, and here’s an example where I do that to fetch two field values from a response body:

final def resp = execution.getVariable("myResponse");
final String status = resp.status.toString();
final String error = resp.error.toString();

Both status and error and respective fields in the said JSON response, like so:

{
    "status": "OK",
    "error": "NONE"
}

I hope this helps :slight_smile:

1 Like