0

According to documentation, the setBody(body) method uses String in the param. But json is a string. If I have a body that I can successfully request using postman with json, how come I'm getting an error that the body is incorrect in salesforce?

This is the sample json class that is in Postman body as JSON

"name": "Apple MacBook Pro 16", "data": { "year": 2019, "price": 1849.99, "CPU model": "Intel Core i9", "Hard disk size": "1 TB" }

The code:

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.restful-api.dev/objects');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
        
String x = '{"name": "Apple MacBook Pro 16","data": {"year": 2019,"price": 1849.99,"CPU model": "Intel Core i9","Hard disk size": "1 TB"}';
String jsonString = JSON.serialize(x);        
        
request.setBody(jsonString);
        
HttpResponse response = http.send(request);

// If the request is successful, parse the JSON response.
System.debug(response.getBody() + '' + response.getStatusCode());

The error log shows: Error: {"error":"400 Bad Request. If you are trying to create or update the data, potential issue is that you are sending incorrect body json or it is missing at all."}400

1
  • Update this String x = '{"name": "Apple MacBook Pro 16","data": {"year": 2019,"price": 1849.99,"CPU model": "Intel Core i9","Hard disk size": "1 TB"}}'; You miss the } at the end of the JSON. Nov 30, 2023 at 12:15

1 Answer 1

0

While this wouldn't be valid in Apex (because we can't have multi-line strings), if you try to "pretty print" your JSON, the issue should become obvious

{
    "name": "Apple MacBook Pro 16",
    "data": {
        "year": 2019,
        "price": 1849.99,
        "CPU model": "Intel Core i9",
        "Hard disk size": "1 TB"
    }

Your JSON string is missing an } at the end. Braces and quotes need to be "balanced" (that is, for every opening brace, you need a corresponding closing brace).

Building a JSON string by just typing it out (as you've tried to do) isn't really recommended because it's easy to make a mistake in all but the simplest of cases. Instead, you can use JSON.serialize() and pass in an instance of an Apex class, or something like a Map<String, Object> or List<Object>.

public class container {
    public String name;
    public Data data;
}

public class Data {
    public Integer year;
    public Decimal price;
    public String cpu_model;
    public String hard_disk_size;
}

Container cont = new Container();
cont.name = 'Apple MacBook Pro 16';
cont.data = new Data();

cont.data.year = 2019;
cont.data.price = 1849.99;
cont.data.cpu_model = 'Intel Core i9';
cont.data.hard_disk_size = '1 TB';

String myJSON = JSON.serialize(cont);
Map<String, Object> jsonObj = new Map<String, Object> {
    'name' => 'Apple MacBook Pro 16',
    'data' => new Map<String, Object> {
        'year' => 2019,
        'price' => 1849.99,
        'CPU Model' => 'Intel Core i9',
        'Hard disk size' => '1 TB'
    }
};

String myJSON = JSON.serialize(jsonObj);
2
  • Hi, thanks for the response I was able to request successfully with the Map. However, why is that even after fixing the String by adding the missing } ... I'm still getting the same error?
    – Kato
    Nov 30, 2023 at 12:57
  • here is the string ... String x = '{"name": "Apple MacBook Pro 16","data": {"year": 2019,"price": 1849.99,"CPU model": "Intel Core i9","Hard disk size": "1 TB"}}';
    – Kato
    Nov 30, 2023 at 12:58

You must log in to answer this question.

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