Check out a free preview of the full Go for JavaScript Developers course:
The "Making an HTTP Request" Lesson is part of the full, Go for JavaScript Developers course featured in this preview video. Here's what you'd learn in this lesson:

Brenna demonstrates how to access the Star Wars API by live coding a function to make a GET request to the API endpoint.

Get Unlimited Access Now

Transcript from the "Making an HTTP Request" Lesson

[00:00:00]
>> Let's create our people route. So I'm gonna kill our print line in main. And create a http.HandleFunc("/people"). And I'm gonna send that getPeople function. So we're gonna find that next func getPeople which takes our favorite two arguments are writer, HTTP response writer. And I request the pointer to http.request.

[00:00:38] And within our function here, for now, we'll we're going to use thumped f print And that takes two things it takes wherever we're writing our text to. And what we wanna put on the page. So I'm gonna say, getting people. If I were to run this now, nothing's gonna happen.

[00:00:58] We're missing a couple things within our main function to actually start up our server. So the first thing I'm going to do is just print serving on 8080. And then Log.Fatal in case this blows up. But we want, Http.ListenAndServe and that takes the port we're talking about, 8080, and any additional, Information which we don't need for this.

[00:01:31] So go ahead and save that guy. Start a server, serving an AD, AD, navigate on over to our browser. And we fire up the browser to slash people you should see whatever you have in your getPeople function handler. Cool, so obviously, we don't just wanna post plain text to the screen, we wanna do some stuff.

[00:01:59] So let's hang out in the getPeople function for a little while. And in here, the first thing we wanna do is handle the get request to the actual API endpoint. So we can use HTTP for this. And we're gonna say HTTP.Get. So the Get requests requires a couple arguments.

[00:02:17] The first is or sorry, just one argument and it's the URL that we want to actually hit. So here we're gonna use our base URL, which we have defined as a constant line 13. And I'm going to concatenate people. So you'll notice that our base URL ends with a forward slash and concatenating slash people, which is the endpoint that we looked at at the beginning on the Star Wars api.co website.

[00:02:47] Base URL, just make sure your URL is in all caps base capital U, capital R, capital L. And then when this function executes, it's going to return two things. The response if everything goes well, and an error if things don't. So because it returns two things, we'll assign those two variables to the left hand side.

[00:03:08] And the first thing we want to do as always, is handle our error. So if the error is not null, we will handle that with http.error, which once again takes a couple things I writer the error and says bad request. I'm just gonna go ahead and print that failed to request Star Wars people.

[00:03:39] Cool, So if everything's fine, let's go ahead and just print out the response and look at what we're working with here. So I'm gonna kill my server and restart it. Navigate back to the browser and refresh. So you'll see my text is no longer there, which is great because we've removed it from the function handler, the route handler.

[00:04:16] And then back in the terminal, you'll see I get a pretty big printout. Most importantly, we see this 200 OK.