I need to get a json from a url,


Here the token is validator for this url, ie it doesnt have any username or password it uses the token to validate and your ip needs to be whitelisted to get a response. I am making this request from a controller.

Http httpProtocol = new Http();
  HttpRequest request = new HttpRequest();
    request.setEndPoint(liveMP); // liveMP is the url with token

        // Set the HTTP verb to GET.
        HttpResponse response = httpProtocol.send(request);// i get an System.HttpResponse[Status=Unauthorized, StatusCode=401] error here
        string resbody = '';
        resbody = response.getBody();

The vendor for that feed has whitelisted the salesforce ips as mentioned in https://help.salesforce.com/apex/HTViewSolution?id=000003652&language=en_US. I can certainly get the feeds when i run the url in the browser.

I have also added the remote site settings. Is there something else i am missing? If a valid token is not provided then it returns out another message but not a 404 which i have checked from browser

  • If you do the same request in the browser do you get a 200 response? Commented Feb 4, 2014 at 13:37
  • how are you creating httpProtocol ? Commented Feb 4, 2014 at 13:39
  • i get the results when i run it on browser. No issues while running from browser
    – Prady
    Commented Feb 4, 2014 at 15:52
  • i havent set any protocol on the code, but liveMP the string i am using to set the endpoint is using http. i have given url bee in the question
    – Prady
    Commented Feb 4, 2014 at 15:55
  • I think your probably would have is that httpProtocol.send(request); should be new http().send(request); Commented Feb 4, 2014 at 16:35

1 Answer 1


Nothing jumps out, so here's how I would debug this issue:

You are getting an HTTPResponse, not an exception, so it appears that your remote site settings are good and you are getting a response for the endpoint.

The endpoint is returning 401 Unauthorized. Possible reasons:

  1. Bad request URL. Copy-pasting URL from your code to a browser may validate.
  2. Wrong protocol. If the endpoint requires HTTPS and your URL uses HTTP.
  3. Bad IP. You said that the provider has whitelisted the SFDC IPs, but perhaps they made an error. If you are able to test from a browser from a non-whitelisted IP (e.g., from home), you can determine if you get the same error.
  4. Some other error at the endpoint. If everything else fails, you may need to ask the provider to check their logs.
  • 1. Bad url request - Copy and paste in browser returns the values. 2. Endpoint uses http and i am passing http though i havent set the protocol explicitly in the code. 3. Non whitelisted ip returns an different error message. 4. I guess i would need to check with the vendor regarding this
    – Prady
    Commented Feb 4, 2014 at 16:01
  • 2
    You didn't mention how you got the token - did you have to log in first in the browser? Could there be cookies in the browser? Commented Feb 4, 2014 at 16:38
  • Token is assigned by the vendor. There is no login process. Anybody with access to this token is given access provided the ip is whitelisted
    – Prady
    Commented Feb 5, 2014 at 10:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .