on 04-09-2020 7:20 PM
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
Hi Naveen,
Please look into the script given in the help.sap.com.
Regards,
Vikas
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Raffael
Thanks !! but is this same applicable for normal response which is not going to exception http-response.png
Thanks
Naveen V
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
Regards
Naveen V
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.
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
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.)
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
85 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.