0

When running the following curl command, I get back a 200

curl --location --request POST '<url hidden>' --header 'Content-Type: application/json' --data-raw '{"images": ["1/2/VB00J9D17kv0CeS4YzL1LKi+hCFqgI9vwufJUWkKBOA=/1.gif"] }'

However when written in apex, I get 400 bad request.

HttpRequest request = new HttpRequest();
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
String value = '{"images": ["1/2/VB00J9Dl7kv0CeS4YzL1LKi+hCFqgI9vwufJUWkKBOA=/1.gif"] }';
request.setEndpoint('<url hidden>');
request.setBody(value);

Http http = new Http();
HttpResponse response = http.send(request);

System.debug(response.getStatusCode() + ' ' + response.getStatus());'

I've also tried sending a body of '{}' on both the curl and the apex, and the curl correctly returns missing body while the apex one still states bad request.

I've tried breaking down to the most basic implementation even hardcoding the body string instead of JSON.stringify or JSONGenerator but no luck. I can't seem to figure out why one would work and the other fail, any help would be deeply deeply appreciated.

additional notes I've tried running this in execute anonymous and through being called from a LWC component (into apex method)

1
  • Have you created remote site settings records for the endpoint url ?
    – MIX DML
    Commented Sep 11, 2022 at 1:17

1 Answer 1

2

Without knowing what your endpoint is, or what server is being used, it's impossible to be 100% certain of this answer. However, I noticed in your curl command that you used the Content-Type of application/json, while in Apex you used application/json;charset=UTF-8. Depending on the service, it may be that it doesn't like the subtype, or doesn't recognize it. Looking elsewhere on the web, I found that at least some servers will complain about this, because they expect application/json; charset=UTF-8 (notice the space), even though the RFCs seem to specify that the space is optional. Try to use the exact same parameters that you used with curl and see if it makes a difference. Even subtle differences like this can break a server.

You must log in to answer this question.

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