Parsing html messages received from the backend

Not applicable

I am getting an error message(404) in text/html format

Can I handle that html message in a RaiseFault Policy?

What I mean is: can I access a specific fields or tag-elements in the html error message?

Solved Solved
1 3 738
1 ACCEPTED SOLUTION

Not applicable

If you are getting this in response.content but not getting a 404 result code then we need to talk to your target system API provider!

In all seriousness though, you can use a Javascript policy to regex or otherwise examine the body of the response and from there set variables, set the response code, or do whatever action you need to trigger the right response - something like the following:

function process(body) {
    if (body === "") {
        //handle malformed response from target on 404
        context.setVariable("response.status.code", 404);
        return "";
    } else {
        try {
            var payload = payloadMediator(JSON.parse(body));
            return JSON.stringify(payload);
        } catch (e) {
            context.setVariable("logError", JSON.stringify({
                error: e.message,
                detail: e.stack
            }));
            throw (e);
        }
    }
}

View solution in original post

3 REPLIES 3

Not applicable

I don't quite understand your question. Is your backend service response a 404 error? If so, wouldn't this be the right response to send back to your client? Or do you want to change your 404 message to something different?

Not applicable

If you are getting this in response.content but not getting a 404 result code then we need to talk to your target system API provider!

In all seriousness though, you can use a Javascript policy to regex or otherwise examine the body of the response and from there set variables, set the response code, or do whatever action you need to trigger the right response - something like the following:

function process(body) {
    if (body === "") {
        //handle malformed response from target on 404
        context.setVariable("response.status.code", 404);
        return "";
    } else {
        try {
            var payload = payloadMediator(JSON.parse(body));
            return JSON.stringify(payload);
        } catch (e) {
            context.setVariable("logError", JSON.stringify({
                error: e.message,
                detail: e.stack
            }));
            throw (e);
        }
    }
}

Yes! Do be careful; parsing HTML is notoriously tricky to do. But it might satisfy if you are looking only for some specific cases.