Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

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

Asked by Camellia Kleiber May 5, 2023 1.7K views 1 answer
Share

About this question

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?

Your answer

1 Answer

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.