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

Trent Ungard
Trent Ungard
10,533 Points

301 error on Treehouse API calls using HTTPS

Hello everyone,

I'm experiencing a bizarre error. When I make a request to https://www.teamtreehouse.com/${username}.json over https, I get a 301 response saying the resource has been moved. I know that this isn't the case, because putting the url for my profile into my browser returns the JSON. I'm using a template literal so it's not an issue with the string I've pasted above, as I've console.logged my url to be sure. I also have another program that uses https to make calls to the treehouse API and it's working fine.

I found other topics from 2015 about when treehouse transitioned from http to https for the API, but as I mentioned, I'm definitely using HTTPS. Hopefully this is just something simple that I'm overlooking but it's driving me crazy. Any help would be appreciated. This is using the node .get method. Thank you!

const https = require('https');
const username = 'trentungard';
const url = `https://www.teamtreehouse.com/${username}.json`;

const getJSON = () => {
    try{
        https.get(url, (res) => {
            if(res.statusCode === 200){
                // some code
            } else {
                // some other code
            }
        })
    } catch(res) {
        console.error(error.message)
    }
}

getJSON();

1 Answer

Trent Ungard
Trent Ungard
10,533 Points

Amazing! Thank you so much1