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 Node.js Basics Building a Command Line Application Getting the Response Body

Adam Lyles Kauffman
Adam Lyles Kauffman
14,016 Points

API not working | 302 STATUS CODE | You are being redirected

Someone make me happy and help me get through this issue!

From the logs we see that the URL is correct. I am calling the function in a post route when the user login it is suppose to grab their profile json.....

Any suggestions is appreciate. If you need more code let me know

Function called with: getUser(user)

getUser Code: function getUser(userdata, apidata){ const request = https.get( "https:/teamtreehouse.com/" + userdata.username +".json", (response) => { let apidata = "" console.dir(response.statusCode) response.on("data", (data) => { console.log(data) apidata += data.toString() }) response.on('end', () =>{ console.log(userdata) console.dir(apidata) return apidata }) } ) }

Log: 302 <Buffer 3c 68 74 6d 6c 3e 3c 62 6f 64 79 3e 59 6f 75 20 61 72 65 20 62 65 69 6e 67 20 3c 61 20 68 72 65 66 3d 22 68 74 74 70 73 3a 2f 2f 74 65 61 6d 74 72 65 ... 64 more bytes> { _id: new ObjectId('66397ba0f80b511d6fedd44d'), fullname: 'i am testing again', username: 'adam5808', password: 'test', createdAt: 2024-05-07T00:53:52.333Z, updatedAt: 2024-05-07T00:53:52.333Z, __v: 0 } '<html><body>You are being <a href="https://teamtreehouse.com/profiles/adam5808.json">redirected</a>.</body></html>'

2 Answers

Adam Lyles Kauffman
Adam Lyles Kauffman
14,016 Points

worked after updating the url to. It seems the redirect was throwing it off for some reason.. "https:/teamtreehouse.com/profiles/" + userdata.username +".json"

Thanks!

Mark Sebeck
Mark Sebeck
Treehouse Moderator 37,763 Points

Adam Lyles Kauffman nice job figuring it out. I made you answer the "Best Answer" because... well it was. And thought it might seem weird for you to make it the best answer yourself. Keep at it!

Travis Alstrand
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travis Alstrand
Data Analysis Techdegree Graduate 47,532 Points

Nice job figuring that out!! Just so you know for the future, always be sure to check the Teacher's Notes below the videos just in case of changes like this. There was a note there in regards to this but you're awesome for figuring that out on your own! :thumbsup: