Hello and welcome to another episode in the Godot Basics tutorial series, Godot Tutorials is not affiliated with or sponsored by Godot Game Engine. In this episode, we will be taking a look at the HTTP request known as a programmer. When dealing with HTTP requests, you will have to deal with and handle your requests based on different situations. The easiest way to deal with HTTP requests is through the HTTP request node.
The HTTP request inherits from the node a class, which means we do have access to the lifecycle scripts and it basically allows us to send and receive HTTP request. The HTTP request node uses the HTTP client class under the hood. We won't be going over HTTP clan class in too much detail in this series. Towards the end of the episode, I will make recommendations to two channels before we get into HTTP request node.
First, let's dive a little bit into the HTTP client class. The client class inherits from reference.
Basically the client class is a low level hypertext transfer protocol client. Now, even though we aren't going into too much detail over the client class, the HTTP client class does come with three enumeration types. The first is method, the second the status, and the third is response code. And we're going to need to understand these enumeration types from HTTP client in order to properly use the HTTP request node.
First is the method enumeration from HTTP client. You have ten choices. The enumeration values are integers zero through nine and each one stands for a different type of request. Or in this case, method, request, method. Get is the value of zero method post has the value of two method put his three method delete. As for and method PETCH is eight in this episode. I won't go over the different types of methods because we went over that in the last episode.
Moving on now, the second type is the enumeration status type and even though you will most likely never use this with the HDTV request. No, let's go over it briefly. The enumeration status type comes with ten different options. I list the ones that might be most appropriate for you that would be disconnected, connecting, connected, requesting and body. Lastly, let's go over the enumeration response code. The values range from integer values. One hundred through five hundred and ten. The one you most likely are familiar with is the two hundred status code, or in this case, the response code.
And this just lets you know that your request has been received and properly responded to when putting information into a server. You may get a tool one response code return back to you by the server for error codes. We do have four hundred for a one and four hundred and three four hundred. Lets you know that something wrong happened with your request. You also have the four one response code, which is just an unauthorized response code. What that means is that even though something exists on the server, you do not have access to it.
And lastly, the four or three response code, which just means that something exists on the server, but it is forbidden for you to access the difference between a four one and four. Three is for one. You have an ID tied to you, whereas for three the server has no clue who you are. And lastly, I do want to point out that there is a response code zero, which is basically a response code error for you. And even though it is not in the documentation, if you are given the response code zero, it does mean that something wrong happened.
Let's move on to HTTP request node enumeration types. So the HTTP request node comes with only one enumeration type code result. The result enumeration type comes with 13 different options. The one you will most likely be familiar with is the integer value zero, which means that a request has been received improperly responded to. If a server cannot be connected to you, you will receive the integer value three, which is result can't resolve. If no response is given, it will give you a integer value. Six If a request failed an integer value eight and if a request has been timed out, you will be given an integer value twelve.
Let's go over a quick view of the request node and how it sends data to a server. So first you use the HTTP request node to request information from a server. When the server has received your request, it will respond appropriately afterwards. Your request will need to do something with the returned data and that's basically it for the HTTP request node now in order to send a request. Yes, to a server, you're going to need to use the request method, the request method takes in five arguments.
The ones highlighted in yellow are the ones you will most likely need to edit for basic usage. The first argument is the string.
You are out of the data you wish to access. This will most likely be your API.
And of course, in the fourth argument you can set what type of method requests you'd like to send to the server. The fifth argument is equally important if you're using post or put, and that is the request data that you wish to send to the server so that it can properly update, create or edit itself. Now, once a request has been sent to a server, a server will respond back. And when is server response data back? The HTTP request node will then call the request completed signal.
So let's go ahead and take a look at the requests completed signal. The request completed signal runs after a response is sent from the server. The request completed signal comes with for arguments that represent different information that the server will respond back to you. The first is going to be the result enumeration type. If everything went OK, this will most likely be the integer value zero. The second argument is the response code enumeration type and it will be an integer value.
Again, the values range from one hundred through five hundred and ten. In most cases. If all went well, you will be receiving the integer value. Two hundred or two hundred and one depending. The third argument are headers and if you need access to the header values that are sent from the server, you will grab that from the third argument called headers. For now, we can just ignore that. And lastly, the data being sent from the server will be in the fourth argument called a body, and it is a pool byte array data type.
And just as the name suggests, you will be given back a pool array of bytes because you are given a pool byte array that a type you will need to do conversion from that into as key or UTF eight. Don't worry about it. The pool byte Ray makes it very simple to convert from pool beta ray into UTF eight encoding. Before we take a look at code, let's go over the results enumeration type versus the response code enumeration type. The result enumeration lets you know whether a server has responded back to you.
This is different than a response code because the response code lets you know what type of server response was sent. Only if a server responds back. Lets go ahead and take a look at a quick example here. We can see that the request completed signal gives us a result value and a response code value. In this case, we sent a request to the server. We responded back successfully. Our result will be zero. But as a matter of fact, we can still get a result enumeration integer value of zero, even if the server responds back with a four or one status code.
And that's because only cares if a server has responded back. The response code cares what type of response is sent back by the server. And so even if we have successfully retrieved information or we have been denied access, our result will still be zero. Now, let's take a look at a second example. Let's say we send a server response to something that doesn't exist. Well, our result will be three. The integer three just means that the server cannot be resolved. And even though it cannot be resolved, our response code in this case will be zero.
And so if you want to do something special for your players, you will do that based on the response code, enumeration value versus the result enumeration value. However, the result enumeration value is useful for knowing whether or not we have in fact connected to the server. So to reiterate, the response code lets us know how the server responded to our request, whereas the result enumeration lets us know whether or not we did in fact connect to a server. For this code example, we will be using the Star Wars API endpoint.
You can use any API endpoint you want to test out HTTP request.
However, I like Star Wars, so we are going to use the Star Wars API now. First, when you grab an API that's publicly available, you need to see how the data is going to be returned back to you. So let's go ahead and take a look at doing that right now. So first, it's letting us know that we need to add https, followed by swapping that dev, followed by API slash, followed by the information we want from our API end point.
Now, this example is letting us know that we need to end with a slash.
And in this example, people one slash returns back to us, Luke Skywalker, to get the name of our person. One, it's under name to get height. It's under.
But basically, it's letting us know that once we receive the JSON file, the information is available straight for us. What this means is it's not nested under something most likely results or data. And I'll show you what I mean by that.
So let's look at something like people.
So if we do, people slash and we press the request button. Notice how the information's different because it's giving us all the people in the API endpoint versus people one which just gives us Luke Skywalker. So in this case, it gives us our account, which helps us iterate the array. We can also iterate an array just by looping over the length of an array, but it's always good to have the count.
And so that's what this information's telling us. The Star Wars API gives us the account.
On top of that, it's also letting us know that all the relevant information is nested under the results. And so results, as you can see, an open bracket means it's an array. And so the first element of the array is Luke Skywalker with the ID one. And if you keep scrolling, you should see C# three people with an I.D. of two and so forth. Now, what's interesting about this API is it comes with the euro and lets us know how to access that specific data.
And if we were to just put that on the screen in the euro section and we were to go ahead and press enter, you can see that we are able to see specifically what we're going to get back or rather what we should expect back from the API endpoint in this case. It's letting us know that in order to retrieve this API endpoint, we need to use the get request method. We should expect back the requests status code of two hundred.
The content type we should be expecting back is application JSON file and this is an important one. It's letting us know what it is allowing.
So it's letting us know that the only request methods this API endpoint will accept is the get request method, the head request method and the options request method. And of course it's letting us know what information we should be expecting for data from everything highlighted. And this is good to know when we want to test out HTTP requests. Note most likely he will have something similar set up for your API endpoint. Again, this episode's not going to talk about creating your own API endpoints.
I'm going to link to different YouTube channels that go over HTTP and server creation. But regardless, let's go ahead and check out how to access this API through the HTTP request node. Now let's go ahead and take a look at an example to add an HTTP request node onto the scene. Go ahead, click on the plus button. You can type it out or find it or regardless, you should find HTTP requests. Go ahead down here and press the create button.
This will add the HTTP request node onto the scene tree. Now something to think about and that is the use thread. If you ever find that your request node is being slow at some point in the game making process, you can in fact turn this on. What this does is when you make an HTTP request through the node, it will in fact create a new threat for you to handle that HTTP request in order to make sure you're not bogging down your main thread. And so even though by default it is set to force, it doesn't hurt to set this to true.
Now, you can either set it through the checkmark or set it through the file and I'll show you how to do both.
So to do it through the inspector, just click that button to get the checkmark and to set it through a script, you're going to call the thread or in this case, the use threads property.
Now, go ahead, head over to your HTTP request note.
Right, click it. Go ahead and select attach script.
It should automatically detect that it is in your request node and select that it inherits from HTTP request. Make sure you do that. You can set the name to whatever you wish to set your gdscript file name to. In this case, I'm going to leave it to the default name HTTP request, which is the name of the node. I'm going to go ahead, press, create, delete all the comments from the screen, double check that you are in fact extending from HTTP request class and we are ready to begin first.
We want to make sure that our node is in fact using threats. So let's go ahead. I'm going to use the self keyword to let you know that we're doing everything through the node class, followed by set use threads and inside we're going to set the value to true. We're going to Pacetti bullion value. True. And this will make sure that are each http request node does in fact. Use threats and threats has the benefit of increasing performance, especially because we do not know how long it's going to take to receive back information.
After that, we are going to need to create a connection. But before we create a connection, we need to connect the signal into a method. And so I'm going to create a new method.
And in this case, I called the method do something, however, we need to give it for different arguments or the function will not work properly or excuse me, the signal will not be able to pass all the relevant information, especially the fourth argument, which is the body of the data that we want to work on or work upon.
So in this case, what you're going to do is make sure you are in fact inside the parentheses.
Remember that the first value is a result. The second is the response code.
The third will be headers and the fourth will be the body, which is a poor beta ray.
And of course, if you want to restrict type the data, you can, in fact, go ahead and do that.
So let's go ahead and do that right now. So the first two values are going to be and this is going to be pool by an array.
And the headers, which we don't really care about, is going to be a pool string array. By doing this, the code becomes clear which argument has what type of data the first two being and the third argument being a pull string, the fourth argument being a pull byte. But because all of this doesn't fit on the screen, I'm going to remove the. Let's move on, so let's add the connection, I'm going to use the self key word to show again that we're using everything inside the array or excuse me, the class.
We're going to use the connect.
Now, we're going to go ahead and remember that the first argument has to be a string value of the signal name. Lucky for us, we do see the request completed. So you can go ahead, either add the string or double click requests, underscore completed. I'm going to go ahead and click that now. Again, requests completed is the signal that gets called once an HTTP request has been responded to by the server. We're going to follow that by a comma. We're going to have to pick our target.
In this case, it's going to be ourselves. So I'm going to use the self keyword for that. And then lastly, we have to pick a method to connect our signal to and as you can see here, gives us some options. However, do something is not listed and that's OK, because we can, in fact, use the string, do something, and it should resolve the error.
Now, let's go ahead and add the request method.
So I'm going to use the soft keyboard again, followed by a request. It is also completed for me. I'm going to just go ahead pick request. We have to pick in this case arguments.
The first argument being the euro, the last being the data in this case, because we're using the get method, all we need to provide is one argument and that will be the string you are.
Oh, so let's go ahead and do that.
If you remember, it's going to be HTTP colon slash slash swap dot dev and that's going to be preceded by a slash, followed by API, followed by another slash.
And in this case, let's go ahead and just grab Luke Skywalker.
And remember, you have to end with a slash because that's what the API documentation was telling us. It needs trailing slashes in your API endpoint. And just if I wasn't clear, the your earlier calling to get data from the API is what we call an API endpoint. So again, everything highlighted here is what we call an API endpoint.
Now, once the server has responded back to us, what's going to happen is we're going to call the request completed signal and we're going to call the do something method inside of our selves.
Then we're going to get back information that would include the result, enumeration value, the response code, enumeration, value header information and most importantly, the body.
So let's go ahead and do that.
Let's just print the body. Let's see if, in fact, this setup works. I'm going to go ahead, press the button.
If we look down here, we can see that, in fact, the request has worked. Now, something interesting has happened. We don't actually see the readable format that I showed you on the website now because the information being given back to us is bytes and we need to convert that into string.
Luckily, the full array data type does in fact come with a method to do that code. Get string from UTF eight. Now, let me go ahead and the fact that this is a poor binary.
At some space and at the very top, we're going to get our data. We're going to get the body followed by the dot notation, followed by jet and we're given auto completes.
And that's because I went ahead and hardcoded that the body is, in fact, a pool biter.
The difference between as key and UTF eight is if you know you're getting bites that fit inside the as key encoding, you can go ahead and choose that to get a bump in speed.
However, I recommend that you use the in this case, get string from UTF because we do not know what we're being given back from the server. And so once we do that, we are converting or bitts into a string, we can go ahead and print the new variable called data inside the print method.
We can go ahead, get rid of pass. When we pressed the play button or we should get back is in fact all the information we saw on the API website for Luke Skywalker in this case, people one now that you saw how to do a get request, let's go ahead and take a look at how to post a request or in this case, how to edit information to an API and point.
Now, we cannot edit API endpoints on the Star Wars API, but let's just pretend that we can. So first, you need to create a variable for the pull string array. In this case, we're going to create a variable headers, call it a pull string.
In this case, it's just an empty array. We don't want to do anything with it in our request method. What we would do is follow the API endpoint in which we wish to update, followed by the comma, followed by custom headers. In this case, we have our headers variable.
Next, we add the bullion value to see if we want to validate the domain.
In this case, we're going to leave it as true, which was the default value.
Now the fourth argument will be the method type in this case, because we're editing already existing data on the API endpoint. We would pick the method put enum value, followed by a comma, followed by the string information we wish to edit on the server.
So for example, we could say name Jane Skinwalker, we're just playing pretend here.
But you would in fact put your data right here and the server side would handle interpreting that data in order to edit it. In this case, we're passing a string value. However, Godot doesn't allow us to send JSON values only string value. So on the server, you would have to interpret the string into JSON most likely. But again, that all depends on the API server. Now, I'm going to go ahead and just comment this out for you, because I'm going to upload this to GitHub or I'm going to add the request back.
Now, let's do something a little more. That's just slightly.
Now, normally we want to do something for the user once we received data. So in this case, that would be the response code. So let's go ahead and do that. Now, normally, we would do something like if our response code is equivalent to Tool one, we're going to actually print the data.
Right. Or in this case, we would actually do everything to make sure that we don't have any errors.
So in this case, if response code is equivalent to the integer value to one or excuse me, in this case, two hundred, because we're not editing anything on the server, we're going to go ahead and convert the body from bytes to string, put that into a variable called data, and then we're going to print everything inside the variable data into the console.
And then else we're going to say that there was a problem on the server.
Now we can actually test this out. Now, we do know that the people has a account of I believe it was eighty two or eighty six. So if we go to 120, we should be fine. Normally that does not exist on the API endpoint. If we press play, what we should get is problem on the server. And of course we'd also like to see what kind of response code were being sent by the server. And so what we're going to do is we're going to actually print response code, followed by Colen, followed by space, followed by the actual response code being returned back to us by the server.
And now if we press play, we can see that, in fact, we're getting a portal for error. The 404 error just means that the data was not found on the server. And so this is just the basic setup of what you are expected to do when you are requesting information from a server. And that's because if the server is down or in this case, if the information does not exist on the server, you want to have a. In this case, a backup plan on what?
To present to your game player user, depending on the response code of the server.
So in this case for the user, we just want to print out to the console the information being sent back to us. And if there's an issue with the server, we just want to let them know that there's a problem with the server. Please try again later. I'm going to recommend to YouTube channels to get more information on Godot and HTTP.
The first YouTube channel is called Game Development Center.
I highly recommend checking this channel out, especially their server series. The second channel is called Generalist Programmer and I highly recommend the HTTP series. I'm going to go ahead and leave links to their YouTube channels and the description down below. Don't forget to follow me on Twitter if you want to see live updates of when I'm editing or shooting a video. And don't forget to subscribe to my subscriber list on my website, Godot Tutorials dot com if you want weekly updates.
Well, that's all I have for you in this episode. Thank you so much for joining me. Thank you for clicking the like button and thank you for clicking the subscribe button. And I look forward to seeing you in the next episode. Have an amazing day.