About

CPI Groovy Examples

Go to List of Examples

Getting HTTP Error Body

This example works if you are using HTTP receiver adapter.

If you are using SOAP adapter you can change adapter type to HTTP, or see the other example "Getting SOAP Error Body"

If you want to convert SOAP Receiver to HTTP Receiver:

  • Change the receiver adapter type. Enter the connection details.

  • Wrap your request message body in SOAP Envelope in a Content Modifier before calling the adapter.

  • (optional) If your service requires, you may need to add the "SOAPAction" HTTP header accordingly.

  • Add an exception subprocess to the flow and use the below Groovy script from the documentation

    Groovy IDE notice You can't test this example with GroovyIDE at the moment.

Script

Try it on Groovy IDE
import com.sap.gateway.ip.core.customdev.util.Message;

//reference:
//https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/a443efe1d5d2403fb95ee9def1a672d4.html

def Message processData(Message message) {

    def map = message.getProperties();

    def ex = map.get("CamelExceptionCaught");
    if (ex!=null) {
        
        // an http adapter throws an instance of org.apache.camel.component.ahc.AhcOperationFailedException
        if (ex.getClass().getCanonicalName().equals("org.apache.camel.component.ahc.AhcOperationFailedException")) {
            
            def messageLog = messageLogFactory.getMessageLog(message);
            messageLog.addAttachmentAsString("http.ResponseBody", ex.getResponseBody(), "text/plain");

            message.setProperty("http.ResponseBody",ex.getResponseBody());
            message.setBody(ex.getResponseBody());

            message.setProperty("http.StatusCode",ex.getStatusCode());
            message.setProperty("http.StatusText",ex.getStatusText());
            
        }
    }

    return message;
}