Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Florian Lindorfer
Florian Lindorfer
16,415 Points

http.get returns "302 Found"

I followed along with the deprecated and actual Node.js course which requires me to GET data from the Treehouse API. In both courses the approach is the same using the httpS requirement, though I receive the status code/message: 302 Found

Here's my code:

const https = require("https");
const username = "flli";


function getProfile() {
  const request = https.get(`https://teamtreehouse.com/profiles/${username}.json`, (response) => {
    console.dir(response.statusMessage);
  });
}

getProfile();
Florian Lindorfer
Florian Lindorfer
16,415 Points

I kept going and received this data:

  <html><body>You are being <a href="https://teamtreehouse.com/home">redirected</a>.</body></html>

Code for reference:

const https = require("https");
const username = "flli";


function getProfile() {
  const request = https.get(`https://teamtreehouse.com/profiles/flli.json`, (response) => {
    let body = "";
    console.dir(response.statusMessage);
    response.on("data", data => {
      body += data.toString();
    });
    response.on("end", () => {
      console.log(body);
    });
  });
}

getProfile();

2 Answers

Florian Lindorfer
Florian Lindorfer
16,415 Points

Can anyone from the treehouse staff have a look please. This seems to be an issue with the API !

Brian Jensen
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Brian Jensen
Treehouse Staff

Florian Lindorfer, I just ran your second code snippet with Node and received a statusMessage of OK and a statusCode of 200. The body was correctly populated with your profile info. Are you testing your code within the Treehouse workspaces, or on your local machine?

Florian Lindorfer
Florian Lindorfer
16,415 Points

Hello Brian, I mainly work in workspaces, though, due to this issue, I also tested it in a local environment which leads to the same outcome.

I also don't use any VPN or proxies which might manipulate the responses.

I tested it on 2 different machines (one of them is completely new) and also via 2 different Wifis and internet providers respectively.

What I could figure out is that I seem to be logged out as the content of the data received says that I'm being redirected to /home. As far as I can recall, I also got the response code for "permanently moved" occassionally.

When I open the json format of a profile in the browser I have no issue to see it, though, when I inspect the network traffic I see the code 304 for profile_name.json.

Florian Lindorfer
Florian Lindorfer
16,415 Points

I ran the exact same code snippet in my local environment again a few minutes ago and it worked now. I didn't make any changes to my setup.