{"id":19,"date":"2021-10-24T15:53:51","date_gmt":"2021-10-24T15:53:51","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/bryanroberts\/?p=19"},"modified":"2021-10-24T15:53:51","modified_gmt":"2021-10-24T15:53:51","slug":"easy-rest-api","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/bryanroberts\/2021\/10\/24\/easy-rest-api\/","title":{"rendered":"Easy REST API"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>When I was first learning about REST API, I struggled to understand what was going on.  REST, which stands for representational state transfer can be easily understood if you look at its two most basic principles &#8211;  endpoint names and using HTTP actions correctly.  REST essentially creates a system where anyone working with the API can easily understand what an endpoint does if you follow those two basic principles.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Endpoints<\/h2>\n\n\n\n<p>First we will talk about endpoints. Let&#8217;s start with an example api that we are creating for a class project.  We have to store quizzes in our api.  Each quiz will have a time limit, name, employee it&#8217;s associated with, and questions.  In a REST api, the basic principal for endpoints is things, not verbs.  Let&#8217;s start with an example of creating the quiz.  We could use any of the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\/createQuiz<\/li><li>\/generateQuiz<\/li><li>\/makeNewQuiz<\/li><li>\/quiz<\/li><\/ul>\n\n\n\n<p>The first three have something in common.  They all describe an action in the name.  They have verbs.  The final one is simply the object itself, the quiz.  Let&#8217;s look at example endpoints we could now use for deleting the quiz.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\/deleteQuiz\/:quiz_id<\/li><li>\/deleteThisQuizQuiz1examplename<\/li><li>\/quiz\/:quiz_id<\/li><\/ul>\n\n\n\n<p>Again, the first two describe an action and then have some way of identifying the quiz that needs to be deleted.  The final endpoint is just the object and the id of the object that needs to be deleted.<\/p>\n\n\n\n<p>The final endpoints in each of these examples is the RESTful endpoint.  They only describe the object that needs to be interacted with.  This is what makes REST so simple and easy to use.  You have one endpoint for the object and then depending on what you want to do with the object, maybe an object id to pinpoint one exact quiz.  But how do we know what action to take with the quiz?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">HTTP Request Type<\/h2>\n\n\n\n<p>This is where the HTTP request type comes in.  The most common HTTP request types include:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>POST &#8211; make a new object<\/li><li>GET &#8211; get a certain object or all objects<\/li><li>DELETE &#8211; delete a certain object or all objects<\/li><\/ul>\n\n\n\n<p>We combine these HTTP requests with the object associated endpoint to interact with the object.  So if we want to create a new quiz it would be:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>POST \/quiz<\/li><\/ul>\n\n\n\n<p>This request would return a unique id for the quiz we just created.  If we want to then get the quiz we created we would call:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>GET \/quiz\/:quiz_id<\/li><\/ul>\n\n\n\n<p>Now if we want to delete that quiz, we could call:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>DELETE \/quiz\/:quiz_id<\/li><\/ul>\n\n\n\n<p>Say we want to get all quizzes, we would call:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>GET \/quiz<\/li><\/ul>\n\n\n\n<p>It&#8217;s this combination of HTTP request type, and object associated endpoints that makes up a RESTful API.  Developers should be able to know exactly what an endpoint does by looking at the endpoint name and the request type it is making .  This simplicity is what makes RESTful APIs so nice to work with.  <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>There are several other factors that go into RESTful APIs such as how data is communicated.  The most common form for data now is the JSON object.  This is usually sent in the request body, and a JSON object is returned when the request is complete.  There&#8217;s also many different HTTP return statuses that will tell the API user if a request was successful, and if not what error occurred.  But the basics of RESTful APIs comes down to the endpoint name and the HTTP request type that is associated with a certain action for that object.  It&#8217;s a very simple system that makes using APIs extremely easy.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction When I was first learning about REST API, I struggled to understand what was going on. REST, which stands for representational state transfer can be easily understood if you look at its two most basic principles &#8211; endpoint names and using HTTP actions correctly. REST essentially creates a system where anyone working with the&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.oregonstate.edu\/bryanroberts\/2021\/10\/24\/easy-rest-api\/\">Continue reading <span class=\"screen-reader-text\">Easy REST API<\/span><\/a><\/p>\n","protected":false},"author":11591,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-19","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/bryanroberts\/wp-json\/wp\/v2\/posts\/19","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/bryanroberts\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/bryanroberts\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/bryanroberts\/wp-json\/wp\/v2\/users\/11591"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/bryanroberts\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":1,"href":"https:\/\/blogs.oregonstate.edu\/bryanroberts\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":20,"href":"https:\/\/blogs.oregonstate.edu\/bryanroberts\/wp-json\/wp\/v2\/posts\/19\/revisions\/20"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/bryanroberts\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/bryanroberts\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/bryanroberts\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}