Groovy Examples

Go to list of examples

Log All the HTTP Headers as JSON

Log All the HTTP Headers as JSON

This is a simple script that will log all headers as pretty JSON. It can be useful for debugging incoming HTTP requests.

Script

Try it on Groovy IDE
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.JsonBuilder;
import groovy.json.JsonOutput;

def Message processData(Message message) {
    def body = message.getBody(java.lang.String) as String;
    
    def builder = new JsonBuilder()
    builder(message.getHeaders())
    def prettyJson = JsonOutput.prettyPrint(builder.toString())
    def messageLog = messageLogFactory.getMessageLog(message);
    if(messageLog != null){
        messageLog.addAttachmentAsString("HTTP Headers:", prettyJson, "application/json");
     }
 
   
     
    return message;
}

Input Headers

Key Value
http_header1 Hello!
http_header2 World!