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
Brian Chipman 4Brian Chipman 4 

Apex Simple Call Out to Endpoint to POST

Would like to create a class to POST some data on an endpoint provided by an outside vendor.  Am modeling it on the "Mighty Moose" Trailhead example (https://trailhead.salesforce.com/en/content/learn/modules/apex_integration_services/apex_integration_rest_callouts) and the only difference seems to be the endpoint, the existence of an "api_key" (which the vendor says should be part of the url), and am passing more than one field.  Looks simple, but I have never done this type of thing before.  

Am testing in the "Open Execute Anonymous Window" of the developer console.  The trailhead example works fine but I am always getting a 400 Bad Request error for the real call out.  I do have a Remote Site setting.

Anything in the code below you can see (some data changed for security reasons)?
Any code you think might work better?
Any questions or details I should ask the vendor?
The vendor says I should be getting an explanatory message, but in the debug log I only see '400 Bad Request' and some other seemingly unrelated stuff.
 
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://my.domain.com/icon/send_eligible_offers?api_key=abcd-1234');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');

// Set the body as a JSON object
request.setBody('{"product_sku":"NTL07007", "serial_number":"1234343434", "user_email":"user.name@gmail.com", "sales_person_email":"agent@mail.com"}');

HttpResponse response = http.send(request);

// Parse the JSON response
if (response.getStatusCode() != 201) {
    System.debug('The status code returned was not expected: ' +
        response.getStatusCode() + ' ' + response.getStatus());
} else {
    System.debug(response.getBody());
}

 
Best Answer chosen by Brian Chipman 4
Egor Gerasimenko 9Egor Gerasimenko 9
400 Bad Request is the response from the endpoint you are trying to hit, and typically indicates that you are sending some bad data. Perhaps you are missing a header or your JSON doesn't match the expected schema. You can try to send the same request using a REST tool like Postman to confirm that the problem is not with the Apex code. 

All Answers

Egor Gerasimenko 9Egor Gerasimenko 9
400 Bad Request is the response from the endpoint you are trying to hit, and typically indicates that you are sending some bad data. Perhaps you are missing a header or your JSON doesn't match the expected schema. You can try to send the same request using a REST tool like Postman to confirm that the problem is not with the Apex code. 
This was selected as the best answer
Brian Chipman 4Brian Chipman 4
Thanks Egor.  We were able to fix and get good 200 responses by removing the charset=UTF-8 portion of the setHeader.