{"id":21827,"date":"2018-09-06T08:51:20","date_gmt":"2018-09-06T13:51:20","guid":{"rendered":"https:\/\/stackify.com\/?p=21827"},"modified":"2024-03-04T06:58:34","modified_gmt":"2024-03-04T06:58:34","slug":"rest-api-tutorial","status":"publish","type":"post","link":"https:\/\/stackify.com\/rest-api-tutorial\/","title":{"rendered":"REST API: Your Guide to Getting Started Quickly"},"content":{"rendered":"<h2><a id=\"post-21827-_vqb0od45wilw\"><\/a>\u00a0What is REST?<\/h2>\n<p>Even though REpresentational State Transfer, also known as REST, is often referred to as a protocol, it\u2019s an architectural style. It defines how applications communicate over the Hypertext Transfer Protocol (HTTP). Applications that use REST are loosely-coupled and transfer information quickly and efficiently. While REST doesn\u2019t define data formats, it\u2019s usually associated with exchanging JSON or XML documents between a client and a server.<\/p>\n<p>We\u2019re going to use a simple service and a web browser to learn about the fundamentals of REST.<\/p>\n<h3>REST Setup<\/h3>\n<p>For this tutorial, you\u2019ll need a system with <a href=\"https:\/\/www.docker.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Docker<\/a> installed. You can find the instructions for your computer <a href=\"https:\/\/www.docker.com\/get-started\" target=\"_blank\" rel=\"noopener noreferrer\">here.<\/a><\/p>\n<p>First, follow the instructions and install <a href=\"https:\/\/stackify.com\/guide-docker-java\/\" target=\"_blank\" rel=\"noopener noreferrer\">Docker<\/a>.<\/p>\n<p>Then, once you\u2019ve completed the installation, you can download and run our sample REST server.<\/p>\n<p>Finally, start the server with this command:<\/p>\n<pre class=\"prettyprint\">$ docker run -p 8080:8080 -d -\u2013name tutorial ericgoebelbecker\/resttutorial\n\nUnable to find image \u2018ericgoebelbecker\/resttutorial:latest\u2019 locally\nlatest: Pulling from ericgoebelbecker\/resttutorial\nff3a5c916c92: Pull complete\n2b5d1cea6e31: Pull complete\nd9ca82731633: Pull complete\n20f20395d7e6: Pull complete\nDigest: sha256:0969a50cbf9511d441c406e0c4e63ea1396aad4dde7729b92747dfae05ed32e2\nStatus: Downloaded newer image for ericgoebelbecker\/resttutorial:latest\n0d971fd363ebe82f9d14ddee84d40dd8f4d7ff42e92d06d07a343c096e4ee37b\n$<\/pre>\n<p><a href=\"https:\/\/stackify.com\/docker-tutorial\/\" target=\"_blank\" rel=\"noopener noreferrer\">Docker<\/a> downloads the server image and runs it. The command tells it to make the web server available on port 8080.<\/p>\n<p>If everything is working, you\u2019ll see a web page like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-21828 size-full\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/08\/rest-api-tutorial-main-page.jpeg\" alt=\"REST API Tutorial Main Page\" width=\"640\" height=\"227\" \/><\/p>\n<p>This is a <a href=\"https:\/\/swagger.io\/\" target=\"_blank\" rel=\"noopener noreferrer\">Swagger page<\/a> that documents the REST API published by this server. We\u2019ll use it to demonstrate how REST APIs are consumed by applications.<\/p>\n<h3><a id=\"post-21827-_68xk1jfxdn21\"><\/a>Introduction to Swagger and REST<\/h3>\n<p>Click on the word <strong>tutorial-controller<\/strong> toward the bottom of the page. It will expand, and you\u2019ll see this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-21829 size-full\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/08\/rest-api-overview.jpeg\" alt=\"REST API Overview\" width=\"640\" height=\"208\" \/><\/p>\n<p>Let\u2019s try a simple request before we start examining each aspect of the API.<\/p>\n<p>Next, click on the blue GET box under <strong>tutorial-controller.\u00a0<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-21830 size-full\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/08\/rest-api-tutorial-get-all.jpeg\" alt=\"REST API Tutorial Get All\" width=\"987\" height=\"499\" \/><\/p>\n<p>This is a description of the <strong>Get All Employees<\/strong> API method. The box documents the status codes it returns, content type, and API path. We\u2019ll cover this in detail as we go. Let\u2019s make a request.<\/p>\n<p>Click the <strong>Try it out!<\/strong> button at the bottom right of the blue shaded area.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-21831 size-full\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/08\/rest-api-tutorial-get-all-result.jpeg\" alt=\"REST API Tutorial Get All Result\" width=\"750\" height=\"725\" \/><\/p>\n<p>Swagger made an API request for us and provides a detailed description of the result. We see three employee records.<\/p>\n<p>Let\u2019s get to work!<\/p>\n<h3><a id=\"post-21827-_h5dnovwslzzg\"><\/a>CRUD!<\/h3>\n<p>The name REpresentational State Transfer implies exchanging data. The server acts as a data store, and the client retrieves and stores data. The server transfers object states to the client. The client can update these states too.<\/p>\n<p>Most REST APIs implement <a href=\"https:\/\/en.wikipedia.org\/wiki\/Create,_read,_update_and_delete\" target=\"_blank\" rel=\"noopener noreferrer\">CRUD<\/a>: Create, Retrieve, Update, and Delete.<\/p>\n<p>Go back to the Swagger page and click on the blue GET box so it collapses. Here&#8217;s a quick tip: at the top of the page, there is the <strong>List Operations<\/strong> option. Clicking there will collapse the operations into a list again.<\/p>\n<p>Let\u2019s look at the list of operations again.<\/p>\n<p>On the left-hand side of the page we see GET, POST, DELETE, GET, PATCH, and PUT. These are HTTP methods that correspond to operations.<\/p>\n<p>We can map these operations into CRUD.<\/p>\n<ul>\n<li>POST\u2014Create<\/li>\n<li>GET\u2014Retrieve<\/li>\n<li>PUT \/ PATCH\u2014Update<\/li>\n<li>DELETE\u2014Delete<\/li>\n<\/ul>\n<p>We\u2019ll cover each operation as we take a look at how REST APIs work.<\/p>\n<h2><a id=\"post-21827-_1pwynu58tmuz\"><\/a>REST API tutorial<\/h2>\n<h3><a id=\"post-21827-_df3jshtcvez1\"><\/a>Create<\/h3>\n<p>Let\u2019s add a new employee to the list.<\/p>\n<p>First, click on the green POST box.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-21832 size-full\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/08\/rest-api-tutorial-create-employee.png\" alt=\"REST API Tutorial Create Employee\" width=\"750\" height=\"592\" \/><\/p>\n<p>In the top right corner, we see the API method name, <strong>Create Employee.<\/strong> This is documentation Swagger extracts from the application code. Many REST Servers use Swagger or a similar web application to document their APIs.<\/p>\n<p>Next, we see information about how the method responds to requests. A successful request will yield an HTTP <strong>Created<\/strong> response code. This is a standard convention for REST APIs.<\/p>\n<p>Under the response information are the request parameters. This API expects a JSON employee record. There\u2019s an example in the box on the right.<\/p>\n<p>Click on the example, and the page will populate the value box on the left for you.<\/p>\n<p>Let\u2019s edit the example.<\/p>\n<pre class=\"prettyprint\">{\n  \u201cemployeeId\u201d: 99,\n  \u201cfirstName\u201d: \u201cSteve\u201d,\n  \u201clastName\u201d: \u201cRogers\u201d,\n  \u201cemail\u201d: \u201ccap@shield.net\u201d,\n  \u201cphone\u201d: \u201c212\u2013555\u20131212\u201d\n}<\/pre>\n<p>Now, click the <strong>Try it out!<\/strong> button.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-21833 size-full\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/08\/rest-api-tutorial-create-results.png\" alt=\"REST API Tutorial Create results\" width=\"750\" height=\"493\" \/><\/p>\n<p>First, at the top of the response, Swagger tells us how we could make the same request using a command line tool called <strong>curl<\/strong>. If you are comfortable with the command line, you can try it out. I am going to omit that part of the responses going forward to save space.<\/p>\n<p>Next, we see details about the request we made.<\/p>\n<p>Then we see the request headers and the response. The response was empty. If this API was more user-friendly, it might return the employee id instead of making us specify it.<\/p>\n<p>Finally, we see the important part. The response code was 201, which corresponds to <strong>Created.<\/strong> The request succeeded!<\/p>\n<p>Let\u2019s go back to the request for all records. Click on the blue GET box on the top again. Then, click on the <strong>Try it out!<\/strong> button.<\/p>\n<p>The response body for this request is the list of employee records the server currently has. Click in the box and you can scroll.<\/p>\n<p>Here are the results:<\/p>\n<pre class=\"prettyprint\">[\n  {\n    \"employeeId\": 1,\n    \"firstName\": \"John\",\n    \"lastName\": \"Doe\",\n    \"email\": \"john@doe.com\",\n    \"phone\": \"555-1212\"\n  },\n  {\n    \"employeeId\": 2,\n    \"firstName\": \"Jenny\",\n    \"lastName\": \"Doe\",\n    \"email\": \"jenny@doe.com\",\n    \"phone\": \"867-5309\"\n  },\n  {\n    \"employeeId\": 3,\n    \"firstName\": \"Clark\",\n    \"lastName\": \"Kent\",\n    \"email\": \"clark@doe.com\",\n    \"phone\": \"555-1213\"\n  },\n  {\n    \"employeeId\": 99,\n    \"firstName\": \"Steve\",\n    \"lastName\": \"Rogers\",\n    \"email\": \"cap@shield.net\",\n    \"phone\": \"212-555-1212\"\n  }\n]<\/pre>\n<p>The last record in the list is the new one we added.<\/p>\n<h3><a id=\"post-21827-_tigrhxqebekq\"><\/a>Failure<\/h3>\n<p>Let\u2019s try to add another employee.<\/p>\n<p>Enter this in the value box:<\/p>\n<pre class=\"prettyprint\">{\n  \"employeeId\": 99,\n  \"firstName\": \"Anthony\",\n  \"lastName\": \"Stark\",\n  \"email\": \"headgenius@stark.com\",\n  \"phone\": \"203-123-4567\"\n}<\/pre>\n<p>Then click <strong>Try it out!<\/strong> again.<\/p>\n<p>Our response code this time was 403, which corresponds to <strong>Forbidden.<\/strong> We can\u2019t add an employee with the same id number.<\/p>\n<p>Here again, a more friendly API might generate ids for us, but this example illustrates how a service enforces data integrity via response codes.<\/p>\n<h3><a id=\"post-21827-_z5347odkhl4p\"><\/a>REST URLs<\/h3>\n<p>Now click on the POST box again to collapse it and click on the second blue one that says GET.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-21834 size-full\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/08\/rest-api-tutorial-retrieve.jpeg\" alt=\"REST API Tutorial Retrieve\" width=\"700\" height=\"424\" \/><\/p>\n<p>This is the API method for retrieving individual employee records.<\/p>\n<p>First, let\u2019s take a look at that URL: <strong>\/api\/tutorial\/1.0\/employees\/{id}<\/strong><\/p>\n<p>It has the same base URL as the one for creating employees. All of the URLs in the API contain <strong>\/api\/tutorial\/1.0\/employees<\/strong>.<\/p>\n<p>HTTP methods are what define operations in well-formed REST APIs, not the URLs<strong>.<\/strong> URLs should not contain verbs.<\/p>\n<p>The difference now is that the id of the employee is part of the URL.<\/p>\n<p>You can think of the URLs as forming an index for the records.<\/p>\n<p>There are four records in the server right now:<\/p>\n<ul>\n<li>\/api\/tutorial\/1.0\/employees\/1<\/li>\n<li>\/api\/tutorial\/1.0\/employees\/2<\/li>\n<li>\/api\/tutorial\/1.0\/employees\/3<\/li>\n<li>\/api\/tutorial\/1.0\/employees\/99<\/li>\n<\/ul>\n<p>So when we want to retrieve, modify, or delete a record, we operate on its URL using the correct HTTP method.<\/p>\n<p>If we want to retrieve all records or add to the set, we operate on the top-level URL: <strong>\/api\/tutorial\/1.0\/employees<\/strong>.<\/p>\n<p>REST URLs usually include versions. I\u2019ve set up this API to be version <strong>1.0<\/strong>: \/api\/tutorial\/<strong>1.0<\/strong>\/employees<\/p>\n<p>This convention provides a migration path for applications. A server can offer two different API versions via different URLs. Client applications can migrate to the new API incrementally or all at once.<\/p>\n<h3><a id=\"post-21827-_qh86m612dhpk\"><\/a>Retrieving data<\/h3>\n<p>Now let\u2019s request an employee. Enter 99 in the value box and click the <strong>Try it out!<\/strong> button.<\/p>\n<p>We get back our new employee and a successful response code.<\/p>\n<pre class=\"prettyprint\">{\n  \"employeeId\": 99,\n  \"firstName\": \"Steve\",\n  \"lastName\": \"Rogers\",\n  \"email\": \"cap@shield.net\",\n  \"phone\": \"212-555-1212\"\n}<\/pre>\n<p>Let\u2019s try an invalid id.<\/p>\n<p>So enter 122 and click the button.<\/p>\n<p>Now we get an error document, and the response code is 500?<\/p>\n<pre class=\"prettyprint\">{\n  \"timestamp\": 1534557371879,\n  \"status\": 500,\n  \"error\": \"Internal Server Error\",\n  \"exception\": \"java.lang.NullPointerException\",\n  \"message\": \"No message available\",\n  \"path\": \"\/api\/tutorial\/1.0\/employees\/122\"\n}<\/pre>\n<p>500 means <strong>Internal Server Error<\/strong>. The error document shows that our server threw a NullPointerException. The service has a bug!<\/p>\n<p>Well-designed REST APIs will handle errors gracefully. In this case, we received a code that makes it clear there is a problem. But if we try another request, the service will respond.<\/p>\n<h3><a id=\"post-21827-_53ox2mwlr0d9\"><\/a>Separation between client and server<\/h3>\n<p>We\u2019ve been exchanging small JSON documents with our server.<\/p>\n<p>Let\u2019s change the contents a bit.<\/p>\n<p>First, go back to the POST area.<\/p>\n<p>Then, enter this in the value text area:<\/p>\n<pre class=\"prettyprint\">{\n  \"employeeId\": 122,\n  \"firstName\": \"Anthony\",\n  \"lastName\": \"Stark\",\n  \"email\": \"headgenius@stark.com\"\n}<\/pre>\n<p>And submit it.<\/p>\n<p>It worked! The server will accept employees with no phone number.<\/p>\n<p>Scroll back up to the first GET box where we can retrieve a list of employees and click the <strong>Try it out!<\/strong> button.<\/p>\n<p>The result includes the new record.<\/p>\n<pre class=\"prettyprint\">[\n  {\n    \"employeeId\": 1,\n    \"firstName\": \"John\",\n    \"lastName\": \"Doe\",\n    \"email\": \"john@doe.com\",\n    \"phone\": \"555-1212\"\n  },\n  {\n    \"employeeId\": 2,\n    \"firstName\": \"Jenny\",\n    \"lastName\": \"Doe\",\n    \"email\": \"jenny@doe.com\",\n    \"phone\": \"867-5309\"\n  },\n  {\n    \"employeeId\": 3,\n    \"firstName\": \"Clark\",\n    \"lastName\": \"Kent\",\n    \"email\": \"clark@doe.com\",\n    \"phone\": \"555-1213\"\n  },\n  {\n    \"employeeId\": 99,\n    \"firstName\": \"Steve\",\n    \"lastName\": \"Rogers\",\n    \"email\": \"cap@shield.net\",\n    \"phone\": \"212-555-1212\"\n  },\n  {\n    \"employeeId\": 122,\n    \"firstName\": \"Anthony\",\n    \"lastName\": \"Stark\",\n    \"email\": \"headgenius@stark.com\",\n    \"phone\": null\n  }\n]<\/pre>\n<p>An important aspect of REST is the separation between client and server.<\/p>\n<p>In CRUD, and therefore REST, the implementation of the client and server are independent. The data forms the demarcation point between the two applications.<\/p>\n<p>In this example, we demonstrated that the server is forgiving when it comes to required and non-required fields in employee records. This is an example of loosely-coupled design.<\/p>\n<p>But we can go further with the separation between client and server. I wrote this example server in Java. It could be replaced with one written in C#, Python, or Ruby. If the URLs and the document remain the same, the client doesn\u2019t have to change.<\/p>\n<h3><a id=\"post-21827-_6abensoj6g9r\"><\/a>Delete<\/h3>\n<p>Let\u2019s remove our incomplete record. Close the POST box and open the DELETE area under it.<\/p>\n<p>The DELETE API method looks a great deal like the GET method. The URL is the same, complete with the employee id. Only the HTTP operation differs.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-21835 size-full\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/08\/rest-api-tutorial-delete.jpeg\" alt=\"REST API Tutorial Delete\" width=\"700\" height=\"426\" \/><\/p>\n<p>Enter 122 in the value text area and make a request. You should receive a 200 response code for success.<\/p>\n<p>Scroll back up the first GET box. Then click the <strong>Try it out!<\/strong> button.<\/p>\n<p>The result is the same list of employees we saw earlier.<\/p>\n<pre class=\"prettyprint\">[\n  {\n    \"employeeId\": 1,\n    \"firstName\": \"John\",\n    \"lastName\": \"Doe\",\n    \"email\": \"john@doe.com\",\n    \"phone\": \"555-1212\"\n  },\n  {\n    \"employeeId\": 2,\n    \"firstName\": \"Jenny\",\n    \"lastName\": \"Doe\",\n    \"email\": \"jenny@doe.com\",\n    \"phone\": \"867-5309\"\n  },\n  {\n    \"employeeId\": 3,\n    \"firstName\": \"Clark\",\n    \"lastName\": \"Kent\",\n    \"email\": \"clark@doe.com\",\n    \"phone\": \"555-1213\"\n  },\n  {\n    \"employeeId\": 99,\n    \"firstName\": \"Steve\",\n    \"lastName\": \"Rogers\",\n    \"email\": \"cap@shield.net\",\n    \"phone\": \"212-555-1212\"\n  }\n]<\/pre>\n<p>Mr. Stark has left the building.<\/p>\n<p>Now go back to the DELETE request area and try an invalid request. Enter 123 for the id.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-21836 size-full\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/08\/rest-api-tutorial-invalid-delete.png\" alt=\"REST API Tutorial Invalid delete\" width=\"750\" height=\"370\" \/><\/p>\n<p>Response code 204 means <strong>no content<\/strong>. Nothing was deleted because we used an id that doesn\u2019t exist.<\/p>\n<h4><a id=\"post-21827-_rnwzmk35om9o\"><\/a>Stateless<\/h4>\n<p>REST APIs are stateless. The server does not need to know about the state of the client and vice versa.<\/p>\n<p>The state of the data is transferred between the applications, just as the name indicates. We added an employee record. We deleted the record.<\/p>\n<p>This decoupling between components has distinct advantages. Many modern <a href=\"https:\/\/stackify.com\/web-application-architecture\/\" target=\"_blank\" rel=\"noopener noreferrer\">web applications<\/a>, including those on phones, use REST. The expense of keeping a connection open and synchronizing state between client and server is too great, especially for an <a href=\"https:\/\/stackify.com\/web-application-problems\/\" target=\"_blank\" rel=\"noopener noreferrer\">application<\/a> with thousands of users.<\/p>\n<p>The disadvantage is that when you design your application, you have to be mindful of the possible synchronization issues between client and server.<\/p>\n<h3><a id=\"post-21827-_ovfgk13jqg29\"><\/a>Updating records<\/h3>\n<p>Let\u2019s add Anthony Stark back to the server again.<\/p>\n<p>Now go back to POST and add this record.<\/p>\n<pre class=\"prettyprint\">{\n  \"employeeId\": 122,\n  \"firstName\": \"Anthony\",\n  \"lastName\": \"Stark\",\n  \"email\": \"headgenius@stark.com\"\n}<\/pre>\n<p>The response code will be 201. So if you go and list all employees, the new record is there.<\/p>\n<p>We&#8217;ll want to add a phone number for this record.<\/p>\n<p>Got to the brown PUT option on the bottom. PUT requires two values, the id of the record to be updated and a document with the new values.<\/p>\n<p>Enter 122 for the id.<\/p>\n<p>Enter a complete record for the employee.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-21837 size-full\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/08\/rest-api-tutorial-first-update.png\" alt=\"REST API Tutorial First Update\" width=\"750\" height=\"282\" \/><\/p>\n<p>Click the button. The response code is 200.<\/p>\n<p>If we list our records again, we see the phone number.<\/p>\n<pre class=\"prettyprint\">[\n  ...\n  {\n    \"employeeId\": 122,\n    \"firstName\": \"Anthony\",\n    \"lastName\": \"Stark\",\n    \"email\": \"headgenius@stark.com\",\n    \"phone\": \"123-456-7890\"\n  }\n]<\/pre>\n<p>Since the id of the record to be updated is a parameter on the path, we can change a record\u2019s id.<\/p>\n<p>So let\u2019s move Mr. Stark to record 100.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-21838 size-full\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/08\/rest-api-tutorial-second-update.png\" alt=\"REST API Tutorial Second update\" width=\"750\" height=\"236\" \/><\/p>\n<p>Next, click the button. The response code is 200.<\/p>\n<p>Then, if we list our records again, we see the new id.<\/p>\n<pre class=\"prettyprint\">[\n  ...\n  {\n    \"employeeId\": 100,\n    \"firstName\": \"Anthony\",\n    \"lastName\": \"Stark\",\n    \"email\": \"headgenius@stark.com\",\n    \"phone\": \"123-456-7890\"\n  }\n]<\/pre>\n<p>Now go back to the PUT area and enter the same request without any changes.<\/p>\n<p>The response code is 204. We tried to modify record 122 again, and it no longer exists.<\/p>\n<h3><a id=\"post-21827-_scammjegjx4a\"><\/a>Patching records<\/h3>\n<p>Submitting partial updates to records is a relatively new operation and is not supported in all APIs.<\/p>\n<p>Some APIs support partial updates via the PUT method, but PATCH is the more technically correct method for partial updates.<\/p>\n<p>So let\u2019s add another incomplete record and then PATCH it.<\/p>\n<p>Add this employee:<\/p>\n<pre class=\"prettyprint\">{\n  \"employeeId\": 123,\n  \"firstName\": \"Donald\",\n  \"email\": \"dahammer@asgard.com\"\n}<\/pre>\n<p>Then, use GET to retrieve employee id 123.<\/p>\n<pre class=\"prettyprint\">{\n  \"employeeId\": 123,\n  \"firstName\": \"Donald\",\n  \"lastName\": null,\n  \"email\": \"dahammer@asgard.com\",\n  \"phone\": null\n}<\/pre>\n<p>We have nulls for both the last name and phone. Let\u2019s add this employee&#8217;s last name.<\/p>\n<p>First, click on the PATCH box.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-21839 size-full\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/08\/rest-api-tutorial-patch-option.png\" alt=\"REST API Tutorial Patch option\" width=\"750\" height=\"232\" \/><\/p>\n<p>The options are identical to a PUT, but we can submit partial records.<\/p>\n<p>Next, enter a record with only a last name. Then, add the employee id of 123.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-21840 size-full\" src=\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/08\/rest-api-tutorial-patch-blake.png\" alt=\"REST API Tutorial Patch Blake\" width=\"750\" height=\"236\" \/><\/p>\n<p>Finally, click the submit button. We will then receive a successful response code.<\/p>\n<p>So let\u2019s retrieve the record again.<\/p>\n<pre class=\"prettyprint\">{\n  \"employeeId\": 123,\n  \"firstName\": \"Donald\",\n  \"lastName\": \"Blake\",\n  \"email\": \"dahammer@asgard.com\",\n  \"phone\": null\n}<\/pre>\n<p>We can update a record by only specifying the fields we wish to add with a PATCH.<\/p>\n<h3><a id=\"post-21827-_c4bexhshcn8\"><\/a>Response codes<\/h3>\n<p>As you can see, it\u2019s critical for client applications to handle HTTP status codes correctly.<\/p>\n<p>Response codes contain three digits. The HTTP standard divides them into ranges.<\/p>\n<p>Codes that begin with 2 indicate success.<\/p>\n<p>A code beginning with 4 means a client error.<\/p>\n<p>A server error begins with 5.<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>Response Code<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td>200<\/td>\n<td>Success<\/td>\n<\/tr>\n<tr>\n<td>201<\/td>\n<td>Created<\/td>\n<\/tr>\n<tr>\n<td>204<\/td>\n<td>No content<\/td>\n<\/tr>\n<tr>\n<td>400<\/td>\n<td>Bad request<\/td>\n<\/tr>\n<tr>\n<td>403<\/td>\n<td>Forbidden<\/td>\n<\/tr>\n<tr>\n<td>404<\/td>\n<td>Not found<\/td>\n<\/tr>\n<tr>\n<td>500<\/td>\n<td>Internal server error<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The <strong>Created<\/strong> code is returned for a new record, while <strong>Success<\/strong> is returned for modifying, deleting, and retrieving records.<\/p>\n<p>The <strong>Forbidden<\/strong> response code indicates a record can\u2019t be created, while <strong>No content<\/strong> indicates a failed modification.<\/p>\n<p>Well-architected APIs will document which code they return under each possible condition.<\/p>\n<p><span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Try Stackify's free code profiler, Prefix, to write better code on your workstation. Prefix works with .NET, Java, PHP, Node.js, Ruby, and Python. &quot;}\" data-sheets-userformat=\"{&quot;2&quot;:769,&quot;3&quot;:{&quot;1&quot;:0},&quot;11&quot;:4,&quot;12&quot;:0}\">Try Stackify&#8217;s free code profiler, <a href=\"https:\/\/stackify.com\/prefix\/\" target=\"_blank\" rel=\"noopener noreferrer\">Prefix<\/a>, to write better code on your workstation. Prefix works with .NET, Java, PHP, Node.js, Ruby, and Python. <\/span><\/p>\n<h3><a id=\"post-21827-_73lz0gwm9hx1\"><\/a>Conclusion<\/h3>\n<p>We\u2019ve used a simple API to examine how REST is used to exchange object state between a client and a server.<\/p>\n<p>While we were adding, removing, updating, and retrieving records, we also looked at how APIs use HTTP methods and response codes to differentiate operations and results.<\/p>\n<p>REST is the <em>lingua franca<\/em> of the modern web. It\u2019s a protocol used for browsers, phones, and IOT devices. Now that you have a basic understanding of how to use it, you\u2019re ready to build the next big thing.<\/p>\n<p>Stackify&#8217;s Application Peformance Management tool, Retrace can help your applications running smoothly with APM, server health metrics, and error log integration.\u00a0 <a href=\"https:\/\/s1.stackify.com\/account\/createclient?_ga=2.12300788.1973545731.1588002198-1971815645.1570122931&amp;_gac=1.207733030.1584390051.EAIaIQobChMIvenD6Oif6AIVnP7jBx3XjACyEAAYBCAAEgJmVPD_BwE\">Download your free two week trial today!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0What is REST? Even though REpresentational State Transfer, also known as REST, is often referred to as a protocol, it\u2019s an architectural style. It defines how applications communicate over the Hypertext Transfer Protocol (HTTP). Applications that use REST are loosely-coupled and transfer information quickly and efficiently. While REST doesn\u2019t define data formats, it\u2019s usually associated [&hellip;]<\/p>\n","protected":false},"author":35,"featured_media":37522,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[7],"tags":[],"class_list":["post-21827","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.6 (Yoast SEO v25.6) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>REST API: Your Guide to Getting Started Quickly - Stackify<\/title>\n<meta name=\"description\" content=\"Understand how to get started quickly with this REST API tutorial. We&#039;ll use a simple service and a web browser to learn about the fundamentals of REST.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/stackify.com\/rest-api-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"REST API: Your Guide to Getting Started Quickly - Stackify\" \/>\n<meta property=\"og:description\" content=\"Understand how to get started quickly with this REST API tutorial. We&#039;ll use a simple service and a web browser to learn about the fundamentals of REST.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/stackify.com\/rest-api-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"Stackify\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Stackify\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-09-06T13:51:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-04T06:58:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/09\/REST-API-Tutorial-881x441-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"881\" \/>\n\t<meta property=\"og:image:height\" content=\"441\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Eric Goebelbecker\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@stackify\" \/>\n<meta name=\"twitter:site\" content=\"@stackify\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Eric Goebelbecker\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/stackify.com\/rest-api-tutorial\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/stackify.com\/rest-api-tutorial\/\"},\"author\":{\"name\":\"Eric Goebelbecker\",\"@id\":\"https:\/\/stackify.com\/#\/schema\/person\/63fa4c0bfdf8cd4fda82eb7e7e69ef93\"},\"headline\":\"REST API: Your Guide to Getting Started Quickly\",\"datePublished\":\"2018-09-06T13:51:20+00:00\",\"dateModified\":\"2024-03-04T06:58:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/stackify.com\/rest-api-tutorial\/\"},\"wordCount\":2218,\"publisher\":{\"@id\":\"https:\/\/stackify.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/stackify.com\/rest-api-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/09\/REST-API-Tutorial-881x441-1.png\",\"articleSection\":[\"Developer Tips, Tricks &amp; Resources\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/stackify.com\/rest-api-tutorial\/\",\"url\":\"https:\/\/stackify.com\/rest-api-tutorial\/\",\"name\":\"REST API: Your Guide to Getting Started Quickly - Stackify\",\"isPartOf\":{\"@id\":\"https:\/\/stackify.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/stackify.com\/rest-api-tutorial\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/stackify.com\/rest-api-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/09\/REST-API-Tutorial-881x441-1.png\",\"datePublished\":\"2018-09-06T13:51:20+00:00\",\"dateModified\":\"2024-03-04T06:58:34+00:00\",\"description\":\"Understand how to get started quickly with this REST API tutorial. We'll use a simple service and a web browser to learn about the fundamentals of REST.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/stackify.com\/rest-api-tutorial\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/stackify.com\/rest-api-tutorial\/#primaryimage\",\"url\":\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/09\/REST-API-Tutorial-881x441-1.png\",\"contentUrl\":\"https:\/\/stackify.com\/wp-content\/uploads\/2018\/09\/REST-API-Tutorial-881x441-1.png\",\"width\":881,\"height\":441},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/stackify.com\/#website\",\"url\":\"https:\/\/stackify.com\/\",\"name\":\"Stackify\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/stackify.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/stackify.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/stackify.com\/#organization\",\"name\":\"Stackify\",\"url\":\"https:\/\/stackify.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/stackify.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/stackify.com\/wp-content\/uploads\/2024\/05\/logo-1.png\",\"contentUrl\":\"https:\/\/stackify.com\/wp-content\/uploads\/2024\/05\/logo-1.png\",\"width\":1377,\"height\":430,\"caption\":\"Stackify\"},\"image\":{\"@id\":\"https:\/\/stackify.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/Stackify\/\",\"https:\/\/x.com\/stackify\",\"https:\/\/www.instagram.com\/stackify\/\",\"https:\/\/www.linkedin.com\/company\/2596184\",\"https:\/\/www.youtube.com\/stackify\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/stackify.com\/#\/schema\/person\/63fa4c0bfdf8cd4fda82eb7e7e69ef93\",\"name\":\"Eric Goebelbecker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/stackify.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/804898064df2edee6dccd64f6ad32c03?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/804898064df2edee6dccd64f6ad32c03?s=96&d=mm&r=g\",\"caption\":\"Eric Goebelbecker\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"REST API: Your Guide to Getting Started Quickly - Stackify","description":"Understand how to get started quickly with this REST API tutorial. We'll use a simple service and a web browser to learn about the fundamentals of REST.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/stackify.com\/rest-api-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"REST API: Your Guide to Getting Started Quickly - Stackify","og_description":"Understand how to get started quickly with this REST API tutorial. We'll use a simple service and a web browser to learn about the fundamentals of REST.","og_url":"https:\/\/stackify.com\/rest-api-tutorial\/","og_site_name":"Stackify","article_publisher":"https:\/\/www.facebook.com\/Stackify\/","article_published_time":"2018-09-06T13:51:20+00:00","article_modified_time":"2024-03-04T06:58:34+00:00","og_image":[{"width":881,"height":441,"url":"https:\/\/stackify.com\/wp-content\/uploads\/2018\/09\/REST-API-Tutorial-881x441-1.png","type":"image\/png"}],"author":"Eric Goebelbecker","twitter_card":"summary_large_image","twitter_creator":"@stackify","twitter_site":"@stackify","twitter_misc":{"Written by":"Eric Goebelbecker","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/stackify.com\/rest-api-tutorial\/#article","isPartOf":{"@id":"https:\/\/stackify.com\/rest-api-tutorial\/"},"author":{"name":"Eric Goebelbecker","@id":"https:\/\/stackify.com\/#\/schema\/person\/63fa4c0bfdf8cd4fda82eb7e7e69ef93"},"headline":"REST API: Your Guide to Getting Started Quickly","datePublished":"2018-09-06T13:51:20+00:00","dateModified":"2024-03-04T06:58:34+00:00","mainEntityOfPage":{"@id":"https:\/\/stackify.com\/rest-api-tutorial\/"},"wordCount":2218,"publisher":{"@id":"https:\/\/stackify.com\/#organization"},"image":{"@id":"https:\/\/stackify.com\/rest-api-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/stackify.com\/wp-content\/uploads\/2018\/09\/REST-API-Tutorial-881x441-1.png","articleSection":["Developer Tips, Tricks &amp; Resources"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/stackify.com\/rest-api-tutorial\/","url":"https:\/\/stackify.com\/rest-api-tutorial\/","name":"REST API: Your Guide to Getting Started Quickly - Stackify","isPartOf":{"@id":"https:\/\/stackify.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/stackify.com\/rest-api-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/stackify.com\/rest-api-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/stackify.com\/wp-content\/uploads\/2018\/09\/REST-API-Tutorial-881x441-1.png","datePublished":"2018-09-06T13:51:20+00:00","dateModified":"2024-03-04T06:58:34+00:00","description":"Understand how to get started quickly with this REST API tutorial. We'll use a simple service and a web browser to learn about the fundamentals of REST.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/stackify.com\/rest-api-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/stackify.com\/rest-api-tutorial\/#primaryimage","url":"https:\/\/stackify.com\/wp-content\/uploads\/2018\/09\/REST-API-Tutorial-881x441-1.png","contentUrl":"https:\/\/stackify.com\/wp-content\/uploads\/2018\/09\/REST-API-Tutorial-881x441-1.png","width":881,"height":441},{"@type":"WebSite","@id":"https:\/\/stackify.com\/#website","url":"https:\/\/stackify.com\/","name":"Stackify","description":"","publisher":{"@id":"https:\/\/stackify.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/stackify.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/stackify.com\/#organization","name":"Stackify","url":"https:\/\/stackify.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/stackify.com\/#\/schema\/logo\/image\/","url":"https:\/\/stackify.com\/wp-content\/uploads\/2024\/05\/logo-1.png","contentUrl":"https:\/\/stackify.com\/wp-content\/uploads\/2024\/05\/logo-1.png","width":1377,"height":430,"caption":"Stackify"},"image":{"@id":"https:\/\/stackify.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Stackify\/","https:\/\/x.com\/stackify","https:\/\/www.instagram.com\/stackify\/","https:\/\/www.linkedin.com\/company\/2596184","https:\/\/www.youtube.com\/stackify"]},{"@type":"Person","@id":"https:\/\/stackify.com\/#\/schema\/person\/63fa4c0bfdf8cd4fda82eb7e7e69ef93","name":"Eric Goebelbecker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/stackify.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/804898064df2edee6dccd64f6ad32c03?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/804898064df2edee6dccd64f6ad32c03?s=96&d=mm&r=g","caption":"Eric Goebelbecker"}}]}},"_links":{"self":[{"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/posts\/21827"}],"collection":[{"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/comments?post=21827"}],"version-history":[{"count":0,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/posts\/21827\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/media\/37522"}],"wp:attachment":[{"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/media?parent=21827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/categories?post=21827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stackify.com\/wp-json\/wp\/v2\/tags?post=21827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}