Why am I getting Status=Bad Request, StatusCode=400 on rest api call?

707    Asked by CamelliaKleiber in Salesforce , Asked on May 5, 2023

Why I am getting a bad request when this executes

Here is my rest api code

@RestResource(urlMapping='/emailservices/*')
global class EmailInsert {
  @HttpPost 
  global static String addEmail(String fName, String fEmail, String pToAddress,
                                    String pSubject, String pTextBody, String cid) {
EmailMessage msg = new EmailMessage(FromName=fName, FromAddress=fEmail, ToAddress=pToAddress,
  Subject=pSubject, TextBody=pTextBody, ParentId=cid, MessageDate=System.now(),
  Incoming=true, Status='0');
insert msg;
return msg.Id;
  }
}
And here is the method that is calling it
  @future(callout=true)
  private static void DoCallout(String cid, String subj, String tbody)
  {
Map jinp = new Map();
jinp.put('fname','Service');
jinp.put('fEmail','noreply@acme.com');
jinp.put('pToAddress', UserInfo.getUserEmail());
jinp.put('pSubject', subj);
jinp.put('pTextBody', tbody);
jinp.put('cid', cid);     
String jsonInput = JSON.serialize(jinp);
    system.debug('JSON String: ' + jsonInput);
    // Call the web service to insert into Email Message object
    Http http       = new Http();
    HttpRequest req = new HttpRequest();
      req.
    req.setHeader('Content-Type', 'application/json');
    req.setEndpoint('https://.force.com/support/services/apexrest/emailservices');
    req.setMethod('POST');
    req.setBody(jsonInput);
    http.send(req);       
  }

When the call executes I end up with Status=Bad Request, StatusCode=400 in the debug logs. I have looked at the formatted JSON String and it looks good.


Any thoughts as to why this fails?

Answered by Clare Matthews

You need to add https://rd-mevion.cs22.force.com in Remote Site Settings even though you might be calling from the same org to stop getting Status=Bad Request, StatusCode=400 on rest api call


Your Answer

Interviews

Parent Categories