1

I have the below JSON POST callout class. The data is sent in an array format whereas the destination requires it in just normal format. please suggest to remove the array in this code an execute in normal format. Please help. I just want it to send data in normal format and not in an array.

public class WebServiceCallout {

    @future (callout=true)
    public static void sendNotification(string Id,string Email,string First_Name,string Last_Name,string phone,string Title,string Account) {
        conweb cont=new conweb(Id,Email,First_Name,Last_Name,Phone,Title,Account);
        List<conweb> conwebs=new List<conweb>();
        conwebs.add(cont);
        HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();
        req.setTimeout(2000); // timeout in milliseconds
        req.setEndpoint('http://127.0.0.1/api/user'); // this is not the real IP address
        req.setMethod('POST');
        req.setHeader('Content-Type','application/json');
        req.setHeader('Authorization','gfkj^%$654GF65yhtd54');
        req.setBody(JSON.serialize(conwebs));
        //req.setBody('Id='+EncodingUtil.urlEncode(Id, 'UTF-8')+'&Email='+EncodingUtil.urlEncode(Email, 'UTF-8')+'&First_Name='+EncodingUtil.urlEncode(First_Name, 'UTF-8')+'&Last_Name='+EncodingUtil.urlEncode(Last_Name, 'UTF-8'));
        //req.setCompressed(true); // otherwise we hit a limit of 32000

        try {
            res = http.send(req);
        } catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
            System.debug(res.toString());
        }
    }
}

1 Answer 1

3

You're asking Apex Code to make a JSON list (array), so that's exactly what it's doing. Just change the line of code where you create the JSON body:

req.setBody(JSON.serialize(cont));

You can remove the conwebs array logic.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.