basiclines / GooglePlay-JSAPI

Google Play Store API in NodeJS
22 stars 18 forks source link

API requests work in browser but not via GET request #7

Closed faysal515 closed 8 years ago

faysal515 commented 8 years ago

Hi there!

I'm having trouble sending GET requests to the API programatically. If I access a URL like [this]() in the browser, I get a JSON response as expected. If I make a GET request in my code, however, I get a 503 Application Error from Heroku. Can you advise on this?

For example, here is what I see with this URL in the browser:

URL works in the browser

And here is what I see if I make a request via the hurl.it service (I get the same error in my code):

URL failing via HTTP request

URL failing via HTTP request

Do I need to include any special HTTP headers with my request or other info? Any advice on how to fix this error?

basiclines commented 8 years ago

Please provide a sample code of the request you are trying out to see if it's related with any CORS issue

faysal515 commented 8 years ago
Meteor.methods({
'fetchFromService': function( ) {
    // Create our future instance.
    var future = new Future();

    HTTP.get( 'https://play-fetch.herokuapp.com/app/com.opera.mini.native.beta', {}, function( error, response ) {
        if ( error ) {
            future.return( error );
        } else {
            future.return( response );
        }
    });

    return future.wait();
}
});

I work in meteor, it's server side method. it returns response but the one that i mentioned in the hurl service.

basiclines commented 8 years ago

Using a vanillajs ajax request works perfectly:


/**
    * Vanilla XMLHttpRequest wrapper
    *
    * @param Object config
        * @param String data
        * @param String url
        * @param Function success
        * @param Function error
    */
    var http = function(config) {
        var req = new XMLHttpRequest();
        var method = (config.data) ? "POST" : "GET";

        req.open(method, config.url, true);
        req.setRequestHeader('User-Agent','XMLHTTP/1.0');

        if (config.data) {
            req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
        }

        req.onreadystatechange = function () {
            if (req.readyState != 4) return;
            if (req.status != 200 && req.status != 304) {
                if ( typeof config.error != "function" ) {
                    console.log(req.status+": "+ req.response);
                } else {
                    config.error(req.status, req.response);
                }
                return;
            }
            config.success(req.response)
        };

        if (req.readyState == 4) return;
        req.send(config.data);
    };
http({
  url: "https://play-fetch.herokuapp.com/app/com.opera.mini.native.beta",
  success: function(data) {
     console.log(data);
  }
})

The only difference i can see here is the UA header setup, maybe Heroku blocks any non-UA identified request. Hope it helps!

faysal515 commented 8 years ago

Thanks. made it work with vanilla JS.