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.
Best Answer
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!