3

I am trying to integration heroku with salesforce using http method on salesfoce. firstly i create heroku postgres dataclip on heroku like "https://dataclips.heroku.com/syneoppllnrictclhumigotdrbsl#employees-1" . then i create a web service class on salesfoce but i got [status = moved Permanently ,statuscode =301 ] error . and res.getbody() and res.getbody() got "You are being redirected."
i write the following code. public class herokuDaaclip {

           @future(callout=true)
             public static void get() {

             HttpRequest req = new HttpRequest();

             //req.setHeader('content-type','application/json;charset=utf-8');
            // https://dataclips.heroku.com/fcroecrluhwltbjinstfqmwyneex.json
            req.setEndpoint('https://postgres.heroku.com/dataclips/syneoppllnrictclhumigotdrbsl.json');
            req.setMethod('GET');

           Http http = new Http();
           HTTPResponse res = http.send(req);
           system.debug('@@@@@@@@@@@@@' + res.getbody());
           if(res.getStatusCode()!=200)
           {
         System.debug('Error from '+ req.getEndpoint() +':'+res.getStatusCode()+''+res.getStatus());
          }


        string resbody = (res.getbody()).replace('','');
         system.debug('fffffffffffff'+resbody);
        // System.debug('Data Clip response code: '+res.getStatusCode()+'. Status: '+res.getStatus());
       // return (List<JSON2Apex>)System.JSON.deserialize(resbody, List<JSON2Apex>.class);

       }
   }
1
  • I'd also like to comment that Heroku is explicitly requesting not to use the dataclips API for production use: Dataclips | Heroku Dev Center You should strictly use it for prototyping or initial testing. Ideally you should implement your own custom API in Heroku. Jan 13, 2015 at 20:14

2 Answers 2

2

Heroku is issuing a permanent redirect that you need to follow. Most tools that help you test APIs annoyingly auto-redirect for you. If you analyze the headers in the 301 response, there is a header called Location and it's value is https://dataclips.s3.amazonaws.com/rjomjlxgmncxulpnuubfdnnddaox.json?response-content-type=application/json&X-Amz-Expires=600&X-Amz-Date=20150113T192826Z&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAILMB7GD6LFWDOJ2Q/20150113/us-east-1/s3/aws4_request&X-Amz-SignedHeaders=host&X-Amz-Signature=0b1304035c32b0e7cce4d55c3aa63f490dd9b27da770a230bd07726426d5a153

Here is a post that outlines how to handle redirects in Salesforce: How do I handle a 302 redirect response to my HTTP callout?

1
  • Thanx Bradley . u gave me right direction ... Jan 14, 2015 at 18:36
0

integration salesforce with heroku dataclip i am creating following code.. and solve 301 status code error . now i got status code 200 (ok). and got heroku data json format.

Code

public class herokuDaaclip {

// public static list x = new list(); // public static string valuese;

 public static void get() {



    HttpRequest req = new HttpRequest();

    //req.setHeader('content-type','application/json;charset=utf-8');
    // https://dataclips.heroku.com/fcroecrluhwltbjinstfqmwyneex.json
    req.setEndpoint('https://dataclips.heroku.com/syneoppllnrictclhumigotdrbsl.json');
    req.setMethod('GET');

    Http http = new Http();
    HTTPResponse res = http.send(req);
    boolean redirect = true;
    if(res.getStatusCode() >=300 && res.getStatusCode() <= 307 && res.getStatusCode() != 306) {
   do {
    redirect = true; // reset the value each time
    String loc = res.getHeader('Location');
    system.debug(':::redirect:::'+loc);
     // 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());

} }

You must log in to answer this question.

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