7

I am trying to make a httpcallout to "https://www.carrierlookup.com". When I tried it with browser's apex rest client extension it returned correct response :

{"Response":{"creditBalance":"985","number":"2067998555","carrier_type":"mobile","carrier":"verizon"}}

but when I tried using apex code and debug response using res.getBody(),it showed html response:

<table border="0" cellpadding="4" cellspacing="0">
<thead>
<tr>
<th>Response</th></tr>
</thead>
<tbody>
<tr>
<td Response='Array'></td><td>Array</td></tr>
</tbody>
</table>

I have used below code for callout :

HttpRequest req = new HttpRequest();
req.setEndpoint(endUrl );
req.setMethod('GET');
req.setHeader('Content-Type', 'text/plain');
req.setTimeout(120000);

Http http = new Http();

HttpResponse res = http.send(req);

Can anyone suggest why it is showing this type of response and How can I get the correct result ?

  • 2
    whats your end point ?looks like headers are wrongly set – Mohith Shrivastava Dec 5 '13 at 2:58
10

Your request's Accept header is probably going out with the value of */* indicating that you can accept any type of response.

This Content-Type header tells the remote endpoint that you're sending them a json request body - it does not indicate anything about the expected response body.

req.setHeader('Content-Type', 'application/json');

Try specifying that the only type of acceptable response is an application/json response.

req.setHeader('Accept', 'application/json');

Relevant W3.org docs: HTTP/1.1: Header Field Definitions

6

Try changing the Content Type Header to

req.setHeader('Content-Type', 'application/json');

----Edit----

So I signed up to their service to see if I could figure it out...although it's no documented anywhere, try adding .json to the end of the endpoint. When I do that, it works for me...

e.g.

req.setEndpoint('http://www.carrierlookup.com/index.php/api/balance?key=[myapikey].json');
  • I have already tried with that but still I am getting html response. Can you please help ? – S.Sharma Dec 5 '13 at 17:34
  • I am trying the other api for lookup. I tried your solution for that but still no success. When I set ('Accept', 'application/json') into the header, it worked. Anyways, thanks for your response. – S.Sharma Dec 6 '13 at 0:12

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.