0

I have narrowed down why my apex post request is not sending to an external API. For some reason the JSON data that I serialize and send is wrong but everything works when I test it using Postman.

    Http http = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('apilink.com');
    req.setMethod('POST');
    req.setHeader('Content-Type', 'application');
    req.setHeader('Authorization', 'token'); 

    String body = '{"gift_id": "12345", "recipients": [{"email": 
    "[email protected]", "fist_name": "Test", "last_name": "test", 
    "message": "Testing post", "type": "email"}]}';
    req.setBody(JSON.serialize(body));
    HttpResponse response = http.send(req);

I get error code 400 which means bad data is being sent.

Any help would be awesome.

4
  • Welcome to Salesforce Stack Exchange. Can you please share the rest (no pun intended) of the pertinent code (you can genericize any sensitive info, like a specific endpoint) - as in, are you calling req.setHeader, req.setMethod, etc.? You can edit your question to add any info.
    – Moonpie
    Commented Jun 18, 2021 at 17:49
  • Added @Moonpie. Thanks! Commented Jun 18, 2021 at 17:57
  • @Ken not sure if this is a typo, but why is there JSON.serialize twice in the code? How r u building the body string? Looks like the string body is correctly formatted in json, try sending the string directly in the request body param
    – Shamina
    Commented Jun 18, 2021 at 18:18
  • @Shamina yup it was a typo. Commented Jun 18, 2021 at 18:28

1 Answer 1

0

Figured it out (Noob mistake).... Did not need to serialize the JSON data. Thanks everyone.

0

You must log in to answer this question.

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