+ Start a Discussion
BBeairdBBeaird 

Null Body For REST HTTP Callout Response

I'm trying to do an HTTP callout for this REST webservice; everything seems to be working except that I'm getting an empty body for the response when I know there should be something there.  If I paste the exact endpoint URL into my browser, I see xml returned.  What could possibly make force.com see an empty response?

 

Code is below.  The webservice is MapAbc's reverse geocode service.  I've left off the last section of the URL which represents our org's key.

 

 

	Http h = new Http();
	HttpRequest req = new HttpRequest();
	
	string url = 'http://search1.mapabc.com/sisserver?highLight=false&config=SPAS&ver=2.0';
	url = url + '&resType=xml';	//Response type
	url = url + '&enc=utf-8&spatialXml=<spatial_request method=\'searchPoint\'>';
	url = url + '<x>106.66423034667969</x>';	//Latitude
	url = url + '<y>26.61029052734375</y>';		//Longitude
	url = url + '<xs></xs><ys></ys>';
	url = url + '<poiNumber>0</poiNumber>';
	url = url + '<range>NaN</range><pattern>0</pattern><roadLevel>0</roadLevel><exkey></exkey></spatial_request>';
	url = url + '&a_k=***OurKey****';	//Key
	url = url + '&a_nocache=615704261174';
	
	req.setEndpoint(url);
    	req.setMethod('GET');    
	HttpResponse res  = new HttpResponse(); 
	res = h.send(req);
	string resBody=res.getBody();
        system.debug('Response Body is: ' + resBody);   //Why is this null???	   

 

Debug logs:

 

08:34:49.056 (56000000)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
08:34:49.056 (56802000)|METHOD_ENTRY|[1]|01pK00000000Nrc|MapAbcTest.MapAbcTest()
08:34:49.056 (56859000)|SYSTEM_MODE_ENTER|false
08:34:49.056 (56909000)|SYSTEM_MODE_EXIT|false
08:34:49.056 (56942000)|METHOD_EXIT|[1]|MapAbcTest
08:34:49.056 (56988000)|CONSTRUCTOR_ENTRY|[1]|01pK00000000Nrc|<init>()
08:34:49.057 (57059000)|SYSTEM_MODE_ENTER|false
08:34:49.057 (57107000)|SYSTEM_MODE_EXIT|false
08:34:49.057 (57166000)|CONSTRUCTOR_EXIT|[1]|01pK00000000Nrc|<init>()
08:34:49.057 (57297000)|METHOD_ENTRY|[2]|01pK00000000Nrc|MapAbcTest.testing()
08:34:49.058 (58417000)|SYSTEM_MODE_ENTER|false
08:34:49.058 (58943000)|SYSTEM_METHOD_ENTRY|[26]|System.HttpRequest.setEndpoint(String)
08:34:49.059 (59003000)|SYSTEM_METHOD_EXIT|[26]|System.HttpRequest.setEndpoint(String)
08:34:49.059 (59081000)|SYSTEM_METHOD_ENTRY|[28]|System.HttpRequest.setMethod(String)
08:34:49.059 (59133000)|SYSTEM_METHOD_EXIT|[28]|System.HttpRequest.setMethod(String)
08:34:49.059 (59240000)|SYSTEM_METHOD_ENTRY|[30]|System.Http.send(APEX_OBJECT)
08:34:49.059 (59350000)|CALLOUT_REQUEST|[30]|System.HttpRequest[Endpoint=http://search1.mapabc.com/sisserver?highLight=false&config=SPAS&ver=2.0&resType=xml&enc=utf-8&spatialXml=<spatial_request method='searchPoint'><x>106.66423034667969</x><y>26.61029052734375</y><xs></xs><ys></ys><poiNumber>0</poiNumber><range>NaN</range><pattern>0</pattern><roadLevel>0</roadLevel><exkey></exkey></spatial_request>&a_k=***OurKey***&a_nocache=615704261174, Method=GET]
08:34:49.287 (287725000)|CALLOUT_RESPONSE|[30]|System.HttpResponse[Status=OK, StatusCode=200]
08:34:49.287 (287820000)|SYSTEM_METHOD_EXIT|[30]|System.Http.send(APEX_OBJECT)
08:34:49.287 (287933000)|SYSTEM_METHOD_ENTRY|[31]|System.HttpResponse.getBody()
08:34:49.287 (287986000)|SYSTEM_METHOD_EXIT|[31]|System.HttpResponse.getBody()
08:34:49.288 (288079000)|SYSTEM_METHOD_ENTRY|[32]|System.debug(ANY)
08:34:49.288 (288154000)|USER_DEBUG|[32]|DEBUG|Response Body is: null
08:34:49.288 (288199000)|SYSTEM_METHOD_EXIT|[32]|System.debug(ANY)
08:34:49.288 (288275000)|SYSTEM_MODE_EXIT|false

 

And finally - this is what I get when I copy and paste that exact endpoint into a browser window:

 

<spatial_response type="SpatialBean" servername="mSIS03" versionname="1.0.9">
<SpatialBean ver="1.0">
<Province ver="1.0">
<name>贵州省</name>
<code>520000</code>
</Province>
<City ver="1.0">
<name>贵阳市</name>
<code>520100</code>
<telnum>0851</telnum>
</City>
<District ver="1.0">
<name/>
<code/>
<x/>
<y/>
<bounds/>
</District>
<roadList type="list">
<Road ver="1.0">
<id>08511789</id>
<name>贵遵高速</name>
<direction>EastNorth</direction>
<distance>38.340862</distance>
<width>12</width>
<ename>Guizun Expressway</ename>
<level>41000</level>
</Road>
<Road ver="1.0">
<id>0851804</id>
<name>服务区</name>
<direction>EastNorth</direction>
<distance>73.932838</distance>
<width>4</width>
<ename>Service area</ename>
<level>43000</level>
</Road>
<Road ver="1.0">
<id>08512255</id>
<name>贵遵高速出口</name>
<direction>WestSouth</direction>
<distance>339.452545</distance>
<width>8</width>
<ename>Guizun Expressway Exit</ename>
<level>44000</level>
</Road>
<Road ver="1.0">
<id>08512254</id>
<name>贵遵高速入口</name>
<direction>WestSouth</direction>
<distance>339.452545</distance>
<width>8</width>
<ename>Guizun Expressway Entrance</ename>
<level>44000</level>
</Road>
<Road ver="1.0">
<id>08511942</id>
<name>兴筑东路</name>
<direction>WestSouth</direction>
<distance>364.536499</distance>
<width>20</width>
<ename>(unnamed road)</ename>
<level>44000</level>
</Road>
</roadList>
<poiList type="list"/>
<crossPoiList type="list">
<cross>
<name>贵遵高速-服务区</name>
<x>106.663529</x>
<y>26.599138</y>
</cross>
</crossPoiList>
</SpatialBean>
</spatial_response>

 

 

vivek bvivek b

I m a newbie in HTTP callout please help me out
I m using the following code to get the result from the endpoint.

From the endpoint i m just sending 'test' as a response.
The result I m getting is System.HttpResponse[Status=null, StatusCode=0] .

public class callOut {
public static HttpRequest buildWebServiceRequest(){
// Build the XML Request
//construct an HTTP request

HttpRequest req = new HttpRequest();
//req.setHeader('content-type', 'text/xml; charset=utf-8');
// req.setHeader('Host','test');
req.setEndpoint('http://domain.com/webservices/testwebservice.php');

req.setMethod('GET');
//req.setBody(body);
return req;
}

public static HttpResponse invokeWebService(Http h, HttpRequest req){
//Invoke Web Service
HttpResponse res = h.send(req);
return res;
}

public static void handleWebServiceResponse(HttpResponse res){
//Parse and apply logic to the res message
System.debug('Status Code: ' + res.getStatusCode());
if (res.getStatusCode() == 200) {

System.debug('Callout body: ' + res.getBody());
} else {
System.debug('Callout failed: ' + res);
}
}



static testMethod void testWebService(){
//build the http request
HttpRequest req = buildWebServiceRequest();
//System.debug('After request: ' + req);
//write apex code to build a sample HttpResponse object
HttpResponse res = new HttpResponse();
//Apply test data and attributes to the HttpResponse object as needed
handleWebServiceResponse(res);
}
}

tggagnetggagne
Did you ever get an answer to this question or fix it youself?  We're experiencing the same problem and are wondering how to get the content.  content-length reads 2215 bytes, but the responseBody is empty.
Jerry ClifftJerry Clifft
I beleive a GET does not have a body, a PUT would have a body.
@GM@GM
The endpoint should be HTTPS not HTTP in case if you are trying to hit from salesforce apex.

check it by setting Content-Type in header?

Regards,
GM