HTTP Request Methods and Status Codes

HTTP Request Methods and Status Codes
Difficulty
Intermediate
Duration
1h 8m
Students
3
Ratings
5/5
starstarstarstarstar
Description

This course takes an in-depth look at Java API for RESTful Web Services, otherwise known as JAX-RS. We also run through some example exam questions that will be useful for anyone who wants to take Oracle's Java EE exam.

Learning Objectives

  • Understand the fundamentals of JAX-RS
  • Learn about the main concepts and components of JAX-RS

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.

Prerequisites

Basic knowledge of Java programming.

Transcript

Hello dear friends. In this video, we will examine HTTP Request Methods and HTTP Status Codes, let's begin. First, let's start with Status Codes. Status Codes are issued by a server in response to a client's request made to the server. All HTT response Status Codes are separated into five classes or categories. The first digit of the Status Code defines the class of response, while the last two digits do not have any classifying or categorization role. There are five classes defined by the standard. Status Codes that start with digit 1 are informational responses and mean that the request was received and is continuing the process. Status Codes that start with a digit 2 are successful responses and indicate that the request has been successfully received, understood, and accepted. Codes that start with digit 3 are redirection messages and indicate that the client is required to take additional steps to complete a request.

Those beginning with digit four are client error responses and indicate that the request contains incorrect syntax or cannot be fulfilled. And for server errors, Status Codes begin with a digit 5 and indicate that the server failed to fulfill an apparently valid request. There are many Status Codes, but we don't use them all. So, I'll focus on the most commonly used ones. Let's start with 1XX Status Codes. 100 is continue, this code indicates that the client can continue with its request. 101 - Switching Protocols. 102 - Processing. 103 - Early Hints. Let's move on to Success Status Codes. 200, is standard response for successful HTTP requests. We can use this in GET operations. 201 - Created, we receive this response when we successfully add data. 202 - Accepted, this response code indicates that the request has been accepted for processing, but the processing has not been completed. 203 - Non-Authoritative Information. 204 - No-Content, the Status Code indicates that the server successfully processed the request and is not returning any content.

Let's now move on to the redirection Status Code class. This class of Status code indicates the client must take additional action to complete the request. In this class, you may use 301 - Moved Permanently. A response of 301, Moved Permanently, means that this and all future requests should be directed to the selected URL. The response to the request can be found under another URI using the GET method. 303 - See other, when received in response to a POST, PUT, or DELETE, the client should presume that the server has received the data and should issue a new GET request to the given URI. Let's continue with Status Codes starting with Digit 4. This class of Status Codes are intended for situations in which the error seems to have been caused by the client. This means that these Codes are extremely important to us. 400 - Bad Request, this code indicates that the request could not be understood by the server.

Bad Request is sent when no other error is applicable, or if the exact error is unknown, or does not have its own error code. 401 - Unauthorized. This code indicates that the requested resource requires authentication. Specifically for use when the authentication is required, and has failed or has not yet been provided. 403 - Forbidden, this code indicates that the request contained valid data and was understood by the server, but the server is refusing action. This may be due to the user not having the necessary permissions for a resource, or needing an account of some sort, or attempting a prohibited action. And finally 404 - Not Found, this code means requested resource does not exist on the server. There are numerous Status Codes in this class of Status Codes, but I believe the ones we went through are enough for us. Response Status Codes beginning with the digit 5 indicate cases in which the server is aware that it has encountered an error or is otherwise incapable of performing the request. At this stage, having knowledge of the 500 - Internal server error is sufficient. A 500 - Internal service error response is essentially a generic error message.

You will come across these Status Codes when an unexpected condition is encountered and no other 500 message is applicable. For now, that's all I have to say about Status Codes. Let's continue with HTTP Methods. HTTP is the underlying format that is used to structure requests and responses for effective communication between a client and a server. The message that is sent by a client to a server is what is known as an HTTP request. When these requests are being sent, clients can use various methods. The primary or most commonly used HTTP methods are; POST, GET, PUT, PATCH, and DELETE. These methods correspond to CREATE, READ, UPDATE, and DELETE operations. Except for them, you may examine HEAD & OPTIONS also. Now, let's examine them theoretically, after that we will make samples with each of them. The GET method requests a representation of the specified resource.

Requests using GET should only retrieve data. For example, we will use GET method to GET a person or person list from any database. Typically the GET method returns two Status Codes from the server. 200 as okay, signifies that you will get value without any issues, or 404, which indicates an invalid or not found ID. Let's go on with POST. The POST method submits an entity to the specified resource, often causing a change in state or side effects on the server. This means with POST method, we will add new items to our database. In success mode, you will get 201 Status code as Created. Also, you can GET 404 code for Not Found. The PUT method replaces all current representations of the target resource with the request payload. The difference between POST and PUT is that PUT requests are idempotent. That is, calling the same PUT request multiple times will always produce the same result.

In contrast calling a POST request repeatedly have side effects of creating the same resource multiple times. If a new resource has been created by the PUT API, the origin server response code will be 201 as Created, and if an existing resource is modified, code will be 200 as Okay, or 204 as No Content. As you may know, the DELETED method is used to delete any data in any server to delete resources. With delete you can get 200 code as Success or 404 as Not Found. The HEAD method asks for a response identical to GET request, but without the response body. In other words, if getUsers return a list of users, then headUsers will make the same request but not return the list of users. HEAD requests are useful for checking what a GET request will return before actually making a GET request like before downloading a large file or response body. And the last one is OPTIONS. The OPTIONS method describes the communication options for the target resource. Now we have finished HTTP methods. In the next video, we will examine the common properties of HTTP methods like save, cacheable, and idempotency. See you in the next video.

About the Author
Students
1300
Courses
64
Learning Paths
4

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics