function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alfia Quadri 11Alfia Quadri 11 

how to use json on httprequest setBody

here is my apex class
I am getting this error

DEBUG|{"status":400,"error":"There was a problem in the JSON you submitted [5132df156cbaf320d27f8e06f3b61656]: logged with error code"}

PLEASE TELL ME HOW TO INCLUDE THE JSON???

public class makeCallOutIC1 {
    public String getContent(String url){
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://api.intercom.io/contacts/search/');
        req.setMethod('POST');
        String authorizationHeader = 'Bearer ' + 'd2222=';
        req.setHeader('Authorization', authorizationHeader);  
        req.setHeader('Accept', 'application/json');
        req.setHeader('Content-Type', 'application/json');
        req.setHeader('Intercom-Version', '2.6');
        String body = '{'+
                       '"query": {' +
              '            "field": "last_seen_at", ' +
              '            "operator": ">", ' +
              '              "value": "1646149510",   ' +
              '        }' +
            '      }';
        

        req.setBody(body);
        HttpResponse res = h.send(req);
        return res.getBody();
        
    }
}
SwethaSwetha (Salesforce Developers) 
HI Alfia,
Does the code fromhttps://salesforce.stackexchange.com/questions/288573/how-to-pass-json-in-set-body-using-rest-and-method-post-in-apex help?
Thanks
Jaya Karthik  karnatiJaya Karthik karnati
Hi Alfia,

you can use JSON Generator to fulfill your requirement

Link for JSON Generator : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_jsongenerator.htm
 
JSONGenerator gen = JSON.createGenerator(true);
		gen.writeStartObject();
        gen.writeFieldName('query');
gen.writeStartObject();
        gen.writeStringField('field', 'last_seen_at');
        gen.writeStringField('operator', '>');
        gen.writeStringField('value','1646149510');
        gen.writeEndObject();        // Get the JSON string.
        String pretty = gen.getAsString();
system.debug('gen-->'+gen);
system.debug('pretty-->'+pretty);

Kindly mark this as best answer if this solves your your issue .

Thanks,
karthik​​​​​​​