cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CPI how to capture Response message from HTTP

navepras
Explorer
0 Kudos

Hi

Can anyone help me out in capturing HTTP response,

here i am stuck in getting message and status from HTTP response which i want to send Email, I have added the content modifier and mail adapter in exception subprocess where it is triggering the Exception and mail also sent, but i could not get the message from the response i tried using CamelhttpStatusCode and CamelhttpText where i am not getting the relevant message which is expected

I have attached the response and status which i got from HTTP

Kindly help me out to capture the message and status

Regards

Naveen V

http-response.png

Accepted Solutions (1)

Accepted Solutions (1)

vikas_agarwal
Contributor

Hi Naveen,

Please look into the script given in the help.sap.com.

https://help.sap.com/viewer/368c481cd6954bdfa5d0435479fd4eaf/Cloud/en-US/a443efe1d5d2403fb95ee9def1a...

Regards,

Vikas

0 Kudos

Blog was very much helpful . Thank you Vikas

Answers (2)

Answers (2)

r_herrmann
Active Contributor

You have to work with the CamelExceptionCaught property which is accessable in ExceptionSubprocesses. This object contains the exception's information. In case of HTTP exceptions you can access the response body via getResponseBody()-function. Call this function and then use e.g. XmlSlurper to parse it.

import com.sap.gateway.ip.core.customdev.util.Message
import java.util.HashMap

def Message processData(Message message) {
    def map = message.getProperties()
    //Get exception object
    def ex = map.get("CamelExceptionCaught")
    if (ex!=null) {  
        //Check if HTTP exception      
        if (ex.getClass().getCanonicalName().equals("org.apache.camel.component.ahc.AhcOperationFailedException")) {
            //Get HTTP status code
            def httpStatusCode = ex.getStatusCode()
            //Parse HTTP response
            def xml = new XmlSlurper().parseText(ex.getResponseBody())
            //Get fields of HTTP response
            def status = xml.'**'.find{it.name() == "status"}.text()
            def message = xml.'**'.find{it.name() == "message"}.text()
            //Set mail text
            def mailText = """HTTP status code: ${httpStatusCode}
Content status: ${status}
Content message: ${message}"""
            //Set body (later use body in mail receiver channel)
            message.setBody(mailText)
        }
    }
    return message
}


navepras
Explorer
0 Kudos

Hi Raffael

Thanks !! but is this same applicable for normal response which is not going to exception http-response.png

Thanks

Naveen V

r_herrmann
Active Contributor
0 Kudos

No, this is especially for exception handling in subprocesses. It wasn't/isn't clear from your question that you wanted a solution for regular cases. Please add a screenshot of the reliant section of you flow and show where/at which place you want to read the response code.

navepras
Explorer
0 Kudos

Hi Raffael

Actually I want to read the response from HTTP when the status is 'ERROR', I want to sent a Email with Message and status which i get from HTTP response

I have attached the IFlow and Response, Usually i will read the Xpath for getting the status and message, here i could not get in that way

Kindly help me out to read the message and status

http-response.png

readresponse.png

Regards

Naveen V

r_herrmann
Active Contributor

You can work with XPath in that case too. Just add the namespace prefix (d:) into your XPath and make the IFlow namespace aware. Otherwise you can take parts from the script I posted above. Just remove the lines for exception handling an directly begin with the XmlSlurper line. Instead of passing the ex.getResponseBody to the XmlSlurper, pass the message body to it.

navepras
Explorer
0 Kudos

Hi Raffael

I tried using the XPath for the uploaded response which is /entry/m:properties/d:status and it is not working

I tried adding the Code which you have sent getting the getResponseBody(), but i am not getting some codes which you provided

Can you help me out understanding what is that xml.'**' Find (i have attached for reference)groovy.png

r_herrmann
Active Contributor
0 Kudos

Hi Naveen,

This page explains how the find-function of the XmlSlurper works. But maybe you should check the other examples on the page too, because they are all really helpful. http://grails.asia/groovy-xmlslurper-examples-for-parsing-xml

Regarding the XPath. Have you added the namespace prefixes to the IFlow as shown here? https://blogs.sap.com/wp-content/uploads/2018/11/NamespaceMappingMultipleBookings.jpg (The screenshot is symbolic. You must add your specific namespace prefixes from your response message there.)

navepras
Explorer
0 Kudos

Hi Raffael

Thanks it working when i added //d: in XPath.

r_herrmann
Active Contributor
0 Kudos

Hi Naveen, thanks for the feedback. I'm glad you figured it out. If my answer helped you to solve your question, feel free the click the "accept" button.

former_member701678
Discoverer

Hi Naveen,

Please feel free to use http://xpather.com/ to get the correct xpaths from the xml message.

Hope it helps.

Best regards,

Sonal