3

I am hoping someone can tell me 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<String, String> jinp = new Map<String, String>();
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://<myinstance>.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?

  • Are you trying to call rest webservice exposed in another Salesforce org?. Because if both classes are in same org, you can just call the method directly instead of using webservice. – Victor May 25 '16 at 20:14
  • can you post JSON String: Debug ? – EricSSH May 25 '16 at 20:41
  • You should be creating a Inner class and serializing the class, not building the string out like that btw developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – EricSSH May 26 '16 at 15:52
  • @EricSSH Thanks I changed the code to use a map object and JSON.serialize(). It definitely created a prettier JSON string but it still does not work – Ohmicron May 26 '16 at 16:52
  • Is this an end point you built? are you authenticating? – EricSSH May 26 '16 at 16:56
0

Are you trying to call rest webservice exposed in another Salesforce org?. Because if both classes are in same org, you can just call the method directly instead of using webservice.

Also if you are trying to access webservice from a different salesforce org, you would need to authenticate against second org using Oauth

Also your end point looks wrong. Generally it will be like "https://rd-mevion.cs22.force.com/services/apexrest/emailservices" without support in between.

Checkout below post, https://developer.salesforce.com/forums/?id=906F000000099zbIAA

  • No it's all under the same org. A community user logged gets a new case page that generates emails. Apparently salesforce doesn't let community users add EmailMessage objects they are read only to them and this cannot be changed. I saw a solution from someone else that they solved it by using a rest api function to insert the email and attach it to the case. the support in the url is because that is part of the community name. If I don't put that I get service unavailable instead of bad request – Ohmicron May 25 '16 at 20:35
  • Can u pls give a try adding token to request using below lines, req.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId()); – Victor May 25 '16 at 20:57
  • I am setting the header to what I thought it had to be to pass the JSON string to the http request as you can see in the code I supplied. If I change the req.setHeader to what you show there I get a different error System.HttpResponse[Status=Unauthorized, StatusCode=401] – Ohmicron May 25 '16 at 20:59
0

You need to add https://rd-mevion.cs22.force.com in Remote Site Settings even though you might be calling from the same org

  • 1
    I had already done that and it made no difference – Ohmicron May 26 '16 at 14:22
0

I had a mismatch in post variable name and parameter in the Rest service. I posted it as fname and retrieved it as fName. The results that came back in the debug logs were very vague that simple "bad request" I used SoapUI to post the request without apex and the results were much more detailed allowing me to figure out why it was a bad request

  • The reason for the bad request it the code throwing an error. SF does not do a good job with error responses thats for sure. – Eric May 27 '16 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.