cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

CPI: Json body With POST method

Akash
Participant
0 Likes
2,831

Hi,

how to pass json body in cpi. consuming a rest API and adding json body with the url with post method. it is taking null value. but i pass this value . how i resolve this kind of issue

Accepted Solutions (0)

Answers (4)

Answers (4)

MartyMcCormick
Product and Topic Expert
Product and Topic Expert
0 Likes

Maybe not the most elegant solution but this works for me with incoming Json payload { "company": "abc123" }

def Message processData(Message message) {
    //Body
/*To set the body, you can use the following method. Refer SCRIPT APIs document for more detail*/
    def messageLog = messageLogFactory.getMessageLog(message);
    def pMap = message.getProperties();
    def body = message.getBody(java.lang.String) as String;
    
   def jsonSlurper = new JsonSlurper()
    input = jsonSlurper.parseText(body)

    def builder = new JsonBuilder();
    builder {
        'CompanyNew'    input.company
        
    }
    
    messageLog.setStringProperty("ResponsePayload", "Printing Payload As Attachment")
    messageLog.addAttachmentAsString("final" , builder.toPrettyString(), "text/plain");

    
    message.setBody(builder.toPrettyString());
    return message;
}

Thanks,

Marty

Akash
Participant
0 Likes

till now it is not not resolved.

Muniyappan
Active Contributor
0 Likes

It does not look like you are passing correct input to this code. try and check your code here

https://groovyide.com/cpi

https://groovyide.com/cpi/share/v1/dVLbioMwEP2VbF5ql5IP6LIvpQ-lULpsX4Ul1alN0UzIxSLiv-9oIvVhV9BJ5py5H...

rrayosdelsol
Explorer
0 Likes

Following! Interested with this topic. I would also like to know the answer to the question asked.