Performing HTTP POST request inside scripted decision node

Does anyone have a good example making POST HTTP request inside ForgeRock “scripted decision node”
including library import.

Hi,
Maybe something like that? In this case I’m making a call with some extra parameters.

var request = new org.forgerock.http.protocol.Request();
request.setUri("SOME_URI");
request.setMethod("POST");
request.getHeaders().add("Content-Type", "application/x-www-form-urlencoded");
var params = "SOME PARAMS" +
    "&param1" +
    "&2";
request.setEntity(params);
var response = httpClient.send(request).get();
logResponse(response);

function logResponse(response) {
    logger.error("HTTP Response: " + response.getStatus() + ", Body: " + response.getEntity().getString());
}
3 Likes

Thank you so much for the reply,

Here is another example of POST request with a public echo endpoint which works by simply copy/paste into “scripted decision node” without changing anything, also added shared state output for convenience. Hope it helps someone.


var fr = JavaImporter(
    org.forgerock.http.protocol.Request,
    org.forgerock.http.protocol.Response,
    org.forgerock.openam.auth.node.api,
    javax.security.auth.callback.TextOutputCallback
);

var uri = 'https://jsonplaceholder.typicode.com/posts';

try {

    var request = new fr.Request();
    request.setUri(uri).setMethod('POST').setEntity(JSON.stringify({
      updatedFields: {key2: 'value2', key3: 'value3'} }
    ));

    request.getHeaders().add('Content-Type', 'application/json; charset=UTF-8');

    var response = httpClient.send(request).getOrThrow();

    sharedState.put("getEntity", response.getEntity().getString())
    sharedState.put("getStatus", response.getStatus())

} catch (e) {
    sharedState.put("error", e)
    throw ('Unable to obtain response from: ' + uri);
}

with (fr) {
    var cbs = [];
    cbs.push(new TextOutputCallback(TextOutputCallback.INFORMATION, "sharedState: ".concat(sharedState.toString())));
    action = Action.send(cbs).build();
}
outcome = "true";