Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

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();

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

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?

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.

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.