21

I am accessing an API, but when i debug the response it shows this message [Status=Moved Temporarily, StatusCode=302]. what should i do to access my required response.. my code is given below

public class WebServiceCall{

    public String getResult{get;set;}

    public PageReference submit() {
        getResult=getData();
        return null;
    }

    public String getData()
    {
        HttpRequest req= new HttpRequest();
         Http http = new Http();
        req.setMethod('GET');
        String url = 'http://www.geocko.com/api/orders/?app_id=xxxxxxxxxxxxx';
        req.setEndpoint(url);
        HttpResponse res = http.send(req);
        String json =  res.getBody();
        System.debug(res.getBody());  
        try {
            JSONObject j = new JSONObject( json );
            return parseJson(j);
        } catch (JSONObject.JSONException e) {
            return 'Error parsing JSON response from Web: '+e;
        }   
    }

    public String parseJson(JSONObject resp){
        return resp.getString('type');
    }
}
6
  • 4
    Welcome to SFSE, wixxey. Please be sure to read the FAQ. In the meantime, can you share some code demonstrating the API call? The more information we have the easier it is to help out.
    – Mike Chale
    Commented Apr 29, 2013 at 12:07
  • So you're calling out from SF?
    – Mike Chale
    Commented Apr 29, 2013 at 12:44
  • Are you sure the endpoint is valid ? It looks like you're successfully connecting to an old endpoint which is no longer operational at that url. It is my understanding that if you receive http status details, you were able to callout, but the problem lies with the service you're calling to. (authentication, parameters, downtime,...). I would only parse json after a status code 200. Commented Apr 29, 2013 at 12:55
  • 1
    Have you tried setting your endpoint to where the 302 is pointing?
    – Mike Chale
    Commented Apr 29, 2013 at 13:02
  • Yes i am calling it from salesforce
    – wixxey
    Commented Apr 30, 2013 at 5:16

1 Answer 1

27

It has been a month since the original post, but hopefully this helps someone else out in the same situation. I handle a similar situation when logging into Site Minder protected pages. I use the code below to deal with sites that throw out an unknown amount of redirects.

Http http = new Http();
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
// do your initial http call here
req.setMethod('GET');
req.setEndpoint('http://www.cloudywithachanceofcode.com/redirect1.php');
res = http.send(req);
// redirection checking
boolean redirect = false;
if(res.getStatusCode() >=300 && res.getStatusCode() <= 307 && res.getStatusCode() != 306) {
    do {
        redirect = false; // reset the value each time
        String loc = res.getHeader('Location'); // get location of the redirect
        if(loc == null) {
            redirect = false;
            continue;
        }
        req = new HttpRequest();
        req.setEndpoint(loc);
        req.setMethod('GET');
        res = http.send(req);
        if(res.getStatusCode() != 500) { // 500 = fail
            if(res.getStatusCode() >=300 && res.getStatusCode() <= 307 && res.getStatusCode() != 306) {
                redirect= true;
            }
            // I do special handling here with cookies
            // if you need to bring a session cookie over to the
            // redirected page, this is the place to grab that info
        }
    } while (redirect && Limits.getCallouts() != Limits.getLimitCallouts());
}
//congratulations you're outside of the redirects now
//read what you need from the res object
system.debug(res.getBody());

It looks for a redirect HTTP response code and then grabs the location from the headers, and repeats the process until it gets a normal response or falls through without a proper response.

You could add some more checks for other status codes as well. Check here for the status codes if you're not familiar with them.

https://developer.mozilla.org/en-US/docs/HTTP/HTTP_response_codes

0

You must log in to answer this question.

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