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 HTTP Basics. HTTP stands for Hypertext Transfer Protocol.
It is a format used to structure, request and responses over the Internet. In a sense, HTTP works as a request and response protocol between a client and a server. The client in most cases is you and your computer and HTTP request is data sent from the client to the server. In a sentence, you would say the server waits for a client request before responding and HTTP response is data sent from a server to the client.
When you send an HTTP request message, you will have to send it with a method. There are many methods provided to an HTTP request message. However, there are five common methods. They are the get method post method, put method patch method and delete method and their name implies what you are requesting from the server. Let's run through all the methods. The get method is used to retrieve a resource. This would be something like a web page.
The Post method is used to create a resource. Think of this as creating a username and password for a web application.
The put method is used to update everything and if that resource does not exist, create it. The patch method is used when you want to partially update an existing resource. And lastly, the delete method is used when you want to delete a resource. Let's take a deeper dive into each individual method. So again, the method can be used to retrieve your resource. Let's say you want to access a webpage. Well, your method would look something like this.
You would have the method type. In this case. We want to use the get method because we just want to retrieve a Web page and then we follow that with the you are of the Web page we wish to retrieve. In this case, we wish to retrieve a Web page called Goodo Tutorials dot com. Now, when we access goodo tutorials dot com, we are returned back a web page resource, in this case an HTML file. Now, when we receive that HTML file, we then request additional resource. If we're HTML file needs additional resource.
This could include external access files, external JavaScript files and so forth. Now normally we would like to use our request methods with an API. An API stands for application programming interface. So let's look at that example. So an API is a url and generally when you access this url and combine it with a request to method, you will expect a certain behavior based on your request method.
So let me give you an example and this. You are provided to us. We need to access these some more API url and provide it with the name of the table we wish to effect. And in this case we wish to effect a table called Persons'. Now, normally APIs don't have to behave like this, but generally they do. And the reason it's set up like this is because when we provide a API URL with some type of request method, we expect a specific behavior to be applied to some resource.
And in this case, that resource would be the person's table. Over here I have a table called persons. In this case over here we have four different types of information and ID number, first name, last name and address. So let's take a look at how we would retrieve some type of information in this case. It's a database with a table called Persons' and C#, the behavior applied to the table based on our request method and our API url. So in this case, with the get method, we wish to retrieve all data in the persons table.
And so when we use the get method, we will get back all the information in this person's table, which would include the ID number of first name, last name and address. Now let's look at how to create a resource. In this case, we would like to add a resource onto the person's table. So we grab the API euro and we pass IT information. Now the post method requires that you provide it all information that the table requires, in this case, a first name, last name and address. And this would be the correct implementation for a post method, although it doesn't have to be followed to the letter T..
Now, let's go ahead and add a resource to the persons table. So in this case, we provided the information John Doe on Earth and as you. And C# here, this information is applied directly into our persons table. Now one thing to note is that the post method expects the server to create the ID number for you. And that's why our post method does not contain an ID number. So, again, Post Method expects that the server creates the ID number for the client.
And so in this case, we have a second row, first name, John, last name to address birth, followed by an ID number of two created by the server. Now let's say we decide to use the post method again with the same exact information. Well, the server will create another ID number for that specific resource information you provide. And in this case, you can see here we have a third row with the same exact information now in many cases. This is the type of behavior you expect the server to behave when you use the post method.
However, if you don't want the server to keep on creating new ID numbers for the same exact information provided, you would use the put method. So they put method, unlike the post method, expects that the client provided the ID number. So in this case, our API has PERSONS slash two and we are saying that in the persons table we would like to access the persons table with the specific row that has the ID number two. And again, the put method requires that you give it all the information.
So in this case, since the table requires a first name, last name and address, it expects a first name, last name and address again. Even though I remove the person's row, this is still the person's table. Now, as you can see here in the table, we do not have an ID number two. However, because we are using the put method, the method will create the resource if the resource does not exist. And so in this case, when we pass it, the first name, last name and address, the server will either update an existing resource or create a new one if it does not exist.
And because the resource did not exist, the server went ahead and created the resource for the persons table with an ID number two, followed by the information John Doe and Earth. Let's go ahead and take a look what happens when we call the put method again. And so when we call the method a second time, you'll notice that we haven't created a third row. And that's because the method requires that you specify the specific resource in which you would like to affect. And in this case, because we have these two, we're saying that we only want to affect the persons table with the ID number two.
And because the number two already exists, the information doesn't change, but the method still goes through. It's still updates everything. But because we gave it the information John Doe on Earth, it will replace all the information with John Doe on Earth. And because it's the same information, we change nothing. But the method is still going through affecting rule number two. Now, let's say we want to change the information with the put method. Well, we keep the API url the same. In this case. We wish to change persons with the ID number two.
And instead of John, we change the first name to Jack. However, because we're using the method, we must provide it the values of everything in the table, which includes first name, last name and address. Even though we do not want to change last name and address, we must still provide that information with the method. And so in this case, when we pass this through to the API server, it will in fact change the first name of the persons table on row number two. However, if you do not wish to provide all the information, you would use the patch method.
The patch method expects only partial information. You do not have to provide all the information, just the specific information you wish to change on the resource. And in this case would be patch method. We wish to revert back the changes we made with the put method and so with the patch method we just provide the first name being John, followed by the Euro API in which we wish to change, in this case, the identification number two. And as you'd expect when we pass this to the API server, the information changes. One thing to note about the patch method is that it does not create a new resource if the resource does not exist.
And that's because the patch method only expects partial information, whereas the post method and the method requires all the information and therefore the post method and the method can go ahead and create an entire row without error because all the information is provided. So something to keep in mind. Post method creates a resource but expects that the server creates the ID number. The put method can create a resource if it does not exist and updates a resource but must be provided all information expected and the patch method only requires the specific information you wish to change on the resource.
However, the patch method cannot create a new resource. And lastly, we have the delete method and in this case we need to. Specify the specific resource in which we want to delete, and in this case, we wish to delete the persons with the identification number two with this specific API. And in this case, you can see down here that even though the resource exists, once we pass this method into the API server, that row gets deleted. And that's basically it for everything on request methods. Now, moving on, we have HTTP response status, quote, These status codes are given to us by the server.
These status codes are useful for knowing what to do on the client site. Well, in order to determine what we do on the client site, we use status codes. So the first status code is information responses. These are numbers one hundred through one hundred ninety nine. The second type of response is successful responses, which are numbers two hundred three two hundred ninety nine. And these are the most common ones. When you access a Web page and you are given back that Web page, you are typically given a status code of two hundred. This lets the browser know that the server did what was expected of it, which is to give you a Web page and therefore your browser can now display the third type of status codes or redirect codes and basically redirect codes.
Lets the in this case client know that it needs to go to a different url address in most cases and redirect codes are from three hundred through three hundred ninety nine. Redirects are useful when a Web page has moved from one URL address to another. If the next type of status codes are client error codes, client error codes range from four hundred through four hundred ninety nine and these codes let you know when it's the client's fault. For example, when the client does not have permission to access a resource, or if a client is trying to access a resource that does not exist.
And lastly, we have server error status codes. And this lets the client know that the information is not being provided to the client because something is wrong on the server. And these server error codes range from five hundred through five hundred ninety nine. Again, status codes are useful when we need to do something on the client site. The next thing you should understand or http headers. Now headers let the client and the server pass additional information with an HTTP request or response HTTP headers or a more advanced topic.
However, let's go through them briefly now. Headers come with a range of information. However, they can be categorized into four different types general headers, request headers, response headers and entity headers. General headers are used in both request and response messages. These can include cache, control, connection and date information. The next type of header is the request header and request. Headers are used to provide information about the request being made. For example, Host and user agent data, which is just your browser you are using along with the computer you are using, for example, Firefox and Macintosh, which is useful to the server to know what kind of information it needs to provide.
Back to you. An example of an http request header using the Host and user agent, for example, is when you want to download a specific application for a specific computer. If you go into Google Chrome's main Web page and you click the download button, you can download it for Linux, Macintosh or Windows. However, wherever you send that information to the server, your computer type and the server responds back with a download link to your specific computer. That's what a request header is useful for.
On top of that, you can also send information like accept and accept language if you want to access different languages on the server, if they provide them. And of course, the method in which you are requesting information for the next type of header is the response letter. The response header is used for the response context. Basically the server contacts, the response headers can include the status code, which we went over previously. It can also provide you information such as content type, for example, is the information HTML file or is it a JSON file? Also, the response header also includes cookies and cache and of course Entity Headers describes the payload of an HTTP request or response message, which includes again, content linked content type and expiration time.
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. I added a simple http get request GitHub project in the description down below, so please feel free to download that and just briefly see how you can make an HTTP request in Godot. And the next episode we're going to take a look at the HTTP Reques node. Thank you so much for joining me and I look forward to seeing you in the next episode. Have an amazing day.