0

I've changed the endpoint url and the json payload for security reasons but let's assume that I've tested the endpoint using advanced rest client and with the same json in my apex code and it works.

myJson: {
  "user" : "test@force.com",
  "pass" : "mypass",
  "url" : "https://login.salesforce.com/services/Soap/u/37.0",
  "type" : "New"
}

My callout looks like this

    HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();
    Http http = new Http();

    String SessionId = Userinfo.getSessionId();
    String strURL = System.URL.getSalesforceBaseURL().toExternalForm();
    req.setEndpoint('https://myapp.herokuapp.com/webapi/myapi');
    req.setMethod('POST');
    req.setHeader('Content-Type', 'application/json');
    JSONGenerator gen = JSON.createGenerator(true);
        gen.writeStartObject();
        gen.writeStringField('user', user);
        gen.writeStringField('pass', pass);
        gen.writeStringField('url', url);
        gen.writeStringField('Type',Type);
        gen.writeEndObject();              
        String myJson = gen.getAsString();
        req.setBody(myJson);

    try {
        req.setTimeout(120000);
        res = http.send(req);
    }

Here are my request and response

CALLOUT_REQUEST|System.HttpRequest[Endpoint=https://myapp.herokuapp.com/webapi/myapi, Method=POST]
CALLOUT_RESPONSE|System.HttpResponse[Status=No Content, StatusCode=204]

I'm having trouble identifying why this isn't working. The issue is, the response is empty. it's supposed to return a json.

  • So, you are saying that same POST request does not work from SF and works from other tool, like postman? – kurunve Feb 16 '17 at 16:47
  • 1
    What exactly is the Problem? Status code 204 says everything worked well. – Andree Wille Feb 16 '17 at 16:53
  • @AndreeWille It didn't return the json i was expecting. it's empty – Tim Tom Feb 16 '17 at 17:02
  • What JSON do you expect. Status Code 204 means: The server successfully processed the request and is not returning any content. – Andree Wille Feb 16 '17 at 17:04
  • @AndreeWille i'm expecting a response from my api. and it's not coming through – Tim Tom Feb 16 '17 at 17:18
0

The issue is that, even though the code is working, the http response body was empty. I set req.setHeader('Accept', 'application/json'); on the request before sending it and that seemed to give me what I wanted. Cheers!

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.