Can't get json application

I tried make a GET request with nodejs but receip the HTML of waiting app…

Are you using axios and cheerio?

Im’ use the module http

Send some code, please.

We can help you debug it - send the error too.

1 Like

this is the request:

http.get({
    host: url.parse("http://night-studio.glitch.me/api/games").host,
    path: url.parse("http://night-studio.glitch.me/api/games").pathname,
    headers:{
        "Content-Type": "application/json"
    }
}, (res) => {
    const { statusCode } = res;
    const contentType = res.headers['content-type'];
    console.log(statusCode);
    let error;
    // Any 2xx status code signals a successful response but
    // here we're only checking for 200.
    if (!/^application\/json/.test(contentType)) {
        error = new Error('Invalid content-type.\n' +
                        `Expected application/json but received ${contentType}`);
    }
    if (error) {
        console.error(error.message);
        // Consume response data to free up memory
        res.resume();
        return;
    }

    res.setEncoding('utf8');
    let rawData = '';
    res.on('data', (chunk) => { 
        rawData += chunk; 
        console.log('c');
    });
    res.on('end', () => {
        try {
            const parsedData = JSON.parse(rawData);
            console.log(parsedData);
        } catch (e) {
        console.error(e.message);
        }
    });
}).on('error', (e) => {
console.error(`Got error: ${e.message}`);
});
1 Like

I faintly remember this being an issue on the forums. You could try looking for a unique header (I think it was X-Starting-Up or something).

I add this to the reuest:

if (res.statusCode != 200) {
    console.log(res.statusCode)
}

and receip 403

403 is the “forbidden” error - what exactly are you trying to access?

To a direction of an API REST

Hum. Maybe you need to send credentials with your request?

how? what credentials?

Hello!

It looks like you’re trying to Getting a JSON response from another glitch Project, Right?

As you need to know, Glitch just banned a ping service. It doesn’t mean only IP has banned, Tho-, But if your user-agent is null, Glitch will throw you to 403 Error!

Soo… How to fix it?

Let’s use request package!

Eh, Deprecated but still working :slight_smile:

And…

//Create Variable
const req = require("request")

//Send request to REST API
//If the User Agent is Blank/null, Glitch will throw it to 403 Error! 
//Then, Let's Set a User-Agent Header!
req("https://rest.api", {
headers: { 
       "User-Agent": "My app - Get res API"
     }
}, function (err, content, body) {

//If there's has Error, Throw it.
if (err) {
throw err;
return false;
}

//Parse the JSON
let res = JSON.parse(body)
//Your code.

})

Let me know if this solve your Issue.

Thank you, yes works

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.