You need to sign in to do that
Don't have an account?
Brian 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.
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()); }
All Answers