0

I have a list of map and list objects. These objects needs to be sent as json content in an http request. The objects are

Map<String,String> leftFileParameters = new Map<String,String>();
    Map<String,String> rightFileParameters = new Map<String,String>();
    Map<String, Object> commonParameters  = new Map<String,Object>();

public void initiateParamas() {
        this.leftFileParameters.put('source_url',leftFile);     
        this.leftFileParameters.put('file_type','pdf');
        this.rightFileParameters.put('source_url',rightFile);
        this.rightFileParameters.put('file_type','pdf');
        this.commonParameters.put('public','false');
        this.commonParameters.put('expiry_time',validUntilTimeStamp);

    }

Now, I have a method as shown below. I want to serialize all the objects shown above and serialize it and set it as the body of request.

public String getURL(){
  initiateParams();
 HttpRequest req = new HttpRequest();
 req.setBody() //cannot serialize here.
}

So, How do i send all the objects as single json and send it in the http request body. Is there any class for doing that?

3

This code will do what you require:

req.setBody(JSON.serialize(new Map<String, Object>{
    'left': leftFileParameters,
    'right': rightFileParameters,
    'common': commonParameters
}));

For more information, See the JSON.serialize method documentation. That method can work on maps and so the above code creates a map to hold the other maps. You can use whatever names you like instead of "left" etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.