2

I want to pass JSON as request Body in rest API request call using the POST method.

JSON:-

{
 "product": {
    "title": "Burton Custom Freestyle 151",
    "body_html": "<strong>Good snowboard!</strong>",
    "vendor": "Burton",
    "product_type": "Snowboard",
    "tags": "Barnes & Noble, John's Fav, \"Big Air\"",
    "images": [
      {
        "src": "http://example.com/rails_logo.gif"
      }
    ]
  }
}

How to do this?

1

2 Answers 2

1

There are couple of ways to do it however best approach would be to create a Wrapper class depicting the structure of your JSON string. For e.g. below would be the format of your JSONWrapper:

public class RequestJSON{
    public productClass product;
    class productClass {
        public String title; //1010101010101010
        public String body_html;   //1
        public String vendor;
        public String product_type;
        public String tags;

        public imagesclass[] images;
        class imagesclass {
            public String src;    //string_data
        }
    }

    public static RequestJSON parse(String json){
        return (RequestJSON) System.JSON.deserialize(json, RequestJSON.class);
    }

}

Once you've JSONWrapper structure finalized, use it to create an Object with the values that you want to send it in JSON request. Once that is done, use the JSON.deserialize to get the JSON string and bind it in the HTTP request body. Your code should look something like this:

// initialize the json object
RequestJSON jsonObj = new RequestJSON ();
jsonObj.product.title = 'Burton Custom Freestyle 151';  
// follow for other vaiables as well.
String jsonBody = json.serialize(jsonObj);        
HttpRequest req = new HttpRequest();
if (jsonBody != null)
    req.setBody(jsonBody);
    req.setHeader('Content-length',string.valueOf(jsonBody.length()));
}
// ... followed by rest of code

Please note that you create wrapper class as per your JSON structure. Above code should give you fair idea exactly how to do it.

1

Assuming you already have the REST API endpoint cleared as a Remote Site (you can check this for reference), then you can try this:

1) Create two inner classes for your JSON structure like this

public class ImageObj {
    public String src;
    public ImageObj(String s) {
        src = s;
    }
}

public class MyPayload {
    public String title;
    public String body_html;
    public String vendor;
    public String product_type;
    public String tags;
    public List<ImageObj> images;
}

2) Define a function to generate a payload like this

private String genPayload(String title, String body, String vendor, String prod_type, String tags, List<String> imgUrls) {
    MyPayload mp = new MyPayload();
    mp.title = title;
    mp.body_html = body;
    mp.vendor = vendor;
    mp.product_type = prod_type;
    mp.tags = tags;
    mp.images = new List<ImageObj>();
    for(String s : imgUrls) {
        mp.images.add(new ImageObj(s));
    }
    return JSON.serialize(mp);
}

3) Finally you'd send the request like this

HttpRequest request = new HttpRequest();
        request.setEndpoint('https://whatever.your.endpoint.is');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setBody(genPayload('Burton...', '..Good snowboard..', 'Burton', 'Snowboard', 'Whatever your tags are', List_of_URLS_here));
        HttpResponse res = h.send(req);
        system.debug('DEBUGGING RESPOSE: ' + res.getStatusCode() + ' > ' + res.getStatus() + '  >  ' + res.getBody());
1
  • 1
    The request.setHeader('Content-Type', 'application/json;charset=UTF-8'); was critical for me. Thank you! Oct 7, 2021 at 19:30

Not the answer you're looking for? Browse other questions tagged .