{"id":1249,"date":"2022-02-25T18:21:36","date_gmt":"2022-02-25T09:21:36","guid":{"rendered":"http:\/\/mylittleforest.kr\/wordpress\/?p=1249"},"modified":"2025-01-15T11:33:03","modified_gmt":"2025-01-15T02:33:03","slug":"restful-api","status":"publish","type":"post","link":"http:\/\/mylittleforest.kr\/wordpress\/blog\/2022\/02\/25\/1249\/","title":{"rendered":"RESTful API"},"content":{"rendered":"\n<h2>RESTful API<\/h2>\n\n\n\n<ul><li>REST \ub780?<\/li><li>Representational State Transfer \uc758 \uc57d\uc790<\/li><li>api \uc5d0 \ub300\ud55c \uc0c1\uc138 \uc2a4\ud399\uc744 \ubcf4\uc9c0 \uc54a\uc544\ub3c4 HTTP\uc694\uccad\uc758 \uc5ec\ub7ec \uc815\ubcf4\ub97c \ud1b5\ud574 API\uac00 \ud558\ub294 \uc77c\uc744 \uc54c \uc218 \uc788\ub3c4\ub85d \uc124\uacc4\ub41c api<\/li><li>REST API\uac00 \ud45c\ud604\ud558\ub294 \uac83<ul><li>\uc790\uc6d0\uacfc \uc790\uc6d0\uc5d0 \ub300\ud55c \ud589\uc704<\/li><li>\uc790\uc6d0 : \uc11c\ubc84\uc5d0 \uc800\uc7a5\ub418\uc5b4 \uc788\ub294 \ub370\uc774\ud130\ub4e4<ul><li>\uc1fc\ud551\ubab0 \uc11c\ube44\uc2a4\uc5d0\uc11c\uc758 \uc790\uc6d0 : \uc0c1\ud488 \ubaa9\ub85d,\uc8fc\ubb38 \ubaa9\ub85d,\ud68c\uc6d0 \ubaa9\ub85d \ub4f1<\/li><li>\ub3c4\uc11c\uad00\ub9ac \uc11c\ube44\uc2a4\uc5d0\uc11c\uc758 \uc790\uc6d0 : \ub3c4\uc11c \uc790\uccb4<\/li><\/ul><\/li><\/ul><\/li><li>REST API\ub97c \ud1b5\ud574 \uc0ac\uc6a9\ud558\ub824\ub294 \uc790\uc6d0\uc744 HTTP uri \ub85c \uba85\uc2dc<\/li><li>HTTP\uba54\uc11c\ub4dc\ub85c \uc790\uc6d0\uc5d0 \ub300\ud55c \ud589\uc704\ub97c \uba85\uc2dc<\/li><\/ul>\n\n\n\n<h3>CRUD<\/h3>\n\n\n\n<ul><li>Create : \uc0dd\uc131(POST)<\/li><li>Read : \uc870\ud68c(GET)<\/li><li>Update : \uc218\uc815(PUT)<\/li><li>Delete : \uc0ad\uc81c(DELETE)<\/li><\/ul>\n\n\n\n<h3>REST API \uc791\uc131 \uaddc\uce59<\/h3>\n\n\n\n<ul><li>\uc790\uc6d0\uc740 URI\uc5d0 \uc601\ubb38 \uc18c\ubb38\uc790, \uba85\uc0ac \ubcf5\uc218\ud615\uc73c\ub85c \ud45c\ud604<\/li><li>\ud30c\uc77c \ud655\uc7a5\uc790\ub294 URI\uc5d0 \ud3ec\ud568\ud558\uc9c0 \uc54a\uc74c<\/li><li>\uc790\uc6d0\uc758 \uacc4\uce35\uad6c\uc870\ub294 \/(\uc2ac\ub798\uc26c)\ub85c \ud45c\ud604<\/li><\/ul>\n\n\n\n<h3>RESTAPI \uc608\uc2dc<\/h3>\n\n\n\n<figure class=\"wp-block-table is-style-regular\">\n<table style=\"width: 100%; height: 229px;\" border=\"2\">\n<thead>\n<tr style=\"height: 39px;\">\n<td style=\"width: 30%; height: 39px;\">CRUD<\/td>\n<td style=\"width: 30%; height: 39px;\">HTTP \uba54\uc11c\ub4dc<\/td>\n<td style=\"width: 40%; height: 39px;\">URI<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"height: 38px;\">\n<td style=\"width: 30%; height: 38px;\">\ucc45\ub4e4\uc758 \ubaa9\ub85d\uc744 \ud45c\uc2dc<\/td>\n<td style=\"width: 30%; height: 38px;\">GET<\/td>\n<td style=\"width: 40%; height: 38px;\">\/books<\/td>\n<\/tr>\n<tr style=\"height: 38px;\">\n<td style=\"width: 30%; height: 38px;\">\ucc45 \ud55c\uad8c\uc758 \uc815\ubcf4\ub97c \ud45c\uc2dc<\/td>\n<td style=\"width: 30%; height: 38px;\">GET<\/td>\n<td style=\"width: 40%; height: 38px;\">\/books\/{id}<\/td>\n<\/tr>\n<tr style=\"height: 38px;\">\n<td style=\"width: 30%; height: 38px;\">\uc0c8 \ucc45\uc744 \uc0dd\uc131<\/td>\n<td style=\"width: 30%; height: 38px;\">POST<\/td>\n<td style=\"width: 40%; height: 38px;\">\/books<\/td>\n<\/tr>\n<tr style=\"height: 38px;\">\n<td style=\"width: 30%; height: 38px;\">\ucc45\uc744 \uc218\uc815<\/td>\n<td style=\"width: 30%; height: 38px;\">PUT<\/td>\n<td style=\"width: 40%; height: 38px;\">\/books\/{id}<\/td>\n<\/tr>\n<tr style=\"height: 38px;\">\n<td style=\"width: 30%; height: 38px;\">\ucc45\uc744 \uc0ad\uc81c<\/td>\n<td style=\"width: 30%; height: 38px;\">DELETE<\/td>\n<td style=\"width: 40%; height: 38px;\">\/books\/{id}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n\n<h3>RESTful API\ub97c \uc0ac\uc6a9\ud558\ub294 \uc774\uc720<\/h3>\n\n\n\n<ul><li>HTTP\ud504\ub85c\ud1a0\ucf5c \uc0c1\uc5d0\uc11c \ub3d9\uc791<\/li><li>\ud2b9\uc815 \uc5b8\uc5b4\ub098 \ud50c\ub7ab\ud3fc\uc5d0 \uc885\uc18d\ub418\uc9c0 \uc54a\uc74c<\/li><li>API \uc758 \uc758\ub3c4\ub97c API\uc0ac\uc6a9\uc790\uac00 \uc27d\uac8c \ud30c\uc545\ud560 \uc218 \uc788\uc74c<\/li><\/ul>\n\n\n\n<h2>Express.js\ub85c RESTful API \ub9cc\ub4e4\uae30<\/h2>\n\n\n\n<ul><li>Express\ub85c HTTP \uba54\uc11c\ub4dc \ucc98\ub9ac<\/li><li>Express\ub85c URI \ucc98\ub9ac<\/li><li>Express\ub85c HTTP \ubc14\ub514 \ucc98\ub9ac<\/li><\/ul>\n\n\n\n<h3><a href=\"http:\/\/expressjs.com\/en\/4x\/api.html\" target=\"_blank\" rel=\"noreferrer noopener\">Express Router<\/a><\/h3>\n\n\n\n<ul><li>\ubbf8\ub4e4\uc6e8\uc5b4\uc758 \ud55c \uc885\ub958<\/li><li>HTTP \uba54\uc11c\ub4dc, uri \uc5d0 \ub530\ub77c \ubbf8\ub4e4\uc6e8\uc5b4 \uc2e4\ud589 \uc5ec\ubd80 \uacb0\uc815<\/li><li>Router \uc0dd\uc131<ul><li>express.Router()\ub85c Express Router \uc0dd\uc131<\/li><\/ul><\/li><li>\uc0dd\uc131\ud55c \ub77c\uc6b0\ud130\ub294 \ub2e4\ub978 \ubbf8\ub4e4\uc6e8\uc5b4\uc640 \ub9c8\ucc2c\uac00\uc9c0\ub85c app.use \ub97c \ud1b5\ud574 \ub4f1\ub85d\ud560 \uc218 \uc788\uc74c<\/li><li>Express Router\ub97c \uc0ac\uc6a9\ud558\uba74 Express\uc571\uc758 \ubaa8\ub4c8\ud654\uac00 \uac00\ub2a5<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n# router.js\n\nvar express = require('express');\nvar router = express.Router();\n\nrouter.get('\/', function(req, res, next) {\n    res.send('GET \/');\n});\n\nrouter.post('\/', function(req, res, next) {\n    res.send('POST \/');\n});\n\nrouter.put('\/', function(req, res, next) {\n    res.send('PUT \/');\n});\n\nrouter.delete('\/', function(req, res, next) {\n    res.send('DELETE \/');\n});\n\nmodule.exports = router;\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n# index.js\n\nvar express = require('express');\nvar app = express();\nvar router = require('.\/router.js')\n\napp.use(router);\n\napp.listen(3000, function() {\n    console.log(&quot;3000 \ud3ec\ud2b8\ub85c \uc6f9\uc11c\ubc84 \uc2e4\ud589!&quot;);\n});\n<\/pre><\/div>\n\n\n<ul><li><a href=\"https:\/\/insomnia.rest\/\" target=\"_blank\" rel=\"noreferrer noopener\">Insomnia \uc124\uce58<\/a><ul><li>\uc11c\ubc84\uc5d0 GET, POST, PUT, DELETE \ub4f1\uc744 \uc804\ub2ec\ud560 \uc218 \uc788\ub294 \uc720\ud2f8\ub9ac\ud2f0<\/li><\/ul><\/li><\/ul>\n\n\n\n<h3>Express Route Parameter<\/h3>\n\n\n\n<ul><li>RESTApi \uc5d0\uc11c \uc790\uc6d0\uc758 \uc544\uc774\ub514\ub97c uri \ub97c \ud1b5\ud574 \uc804\ub2ec<\/li><li>\uc544\uc774\ub514\uc640 \uac19\uc774 Uri\uc758 \ubcc0\ud558\ub294 \uc694\uc18c\ub97c Route prameter \ub77c\uace0 \ud568<\/li><li>Express Router \uc5d0\uc11c\ub294 :(\ucf5c\ub860)\uacfc \ud30c\ub77c\ubbf8\ud130 \uc774\ub984\uc73c\ub85c Route parameter\ub97c \uc124\uc815<\/li><li>Request \uac1d\uccb4\uc758 params \ud504\ub85c\ud37c\ud2f0\ub85c Route parameter\uc5d0 \uc811\uadfc<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nrouter.get('\/:id', function(req, res, next) {\n    res.send('\uc804\ub2ec\ubc1b\uc740 \ud30c\ub77c\ubbf8\ud130 : ' + req.params.id);\n});\n<\/pre><\/div>\n\n\n<ul><li>GET localhost:3000  -&gt; \uc751\ub2f5 : Cannot GET\/<\/li><li>GET localhost:3000\/13244  -&gt; \uc751\ub2f5 : \uc804\ub2ec\ubc1b\uc740 \ud30c\ub77c\ubbf8\ud130 : 13244<\/li><\/ul>\n\n\n\n<h3>Query String<\/h3>\n\n\n\n<ul><li>URI \ub97c \ud1b5\ud574 \uac12\uc744 \uc804\ub2ec\ud558\ub294 \ub610 \ud558\ub098\uc758 \ubc29\ubc95<\/li><li>RESTAPI \uc5d0\uc11c\ub294 \uc8fc\ub85c \uc790\uc6d0\uc744 \uac80\uc0c9, \ud544\ud130\ub9c1 \ud560 \ub54c \uc0ac\uc6a9<\/li><li>ex) \/books?author=\uae40\uad70\uc624&amp;language=kr<\/li><\/ul>\n\n\n\n<h3>Express\uc5d0\uc11c Query String \uc0ac\uc6a9<\/h3>\n\n\n\n<ul><li>Request \uac1d\uccb4\uc758 query \ud504\ub85c\ud37c\ud2f0\ub85c \uc811\uadfc<\/li><li>Route parameter \uc640 \ub2e4\ub974\uac8c \uc5b4\ub5a4 query string\uc744 \ubc1b\uc744\uc9c0 \ubbf8\ub9ac \uc124\uc815\ud558\uc9c0 \uc54a\uc74c<\/li><li>\ub77c\uc6b0\ud2b8\uc5d0 \uc601\ud5a5\uc744 \uc8fc\uc9c0 \uc54a\uc74c<\/li><li>\ucffc\ub9ac\uc2a4\ud2b8\ub9c1\uc758 \uc720\ubb34\uc640 \uad00\uacc4\uc5c6\uc774 http \uba54\uc11c\ub4dc\uc640 uri \ub97c \ud1b5\ud574\uc11c\ub9cc \ub77c\uc6b0\ud2b8\uac00 \uacb0\uc815<\/li><li>\uc804\ub2ec\ud558\uc9c0 \uc54a\uc740 query string\uc5d0 \uc811\uadfc\ud558\uba74 \uac12\uc774 \uc124\uc815\ub418\uc5b4 \uc788\uc9c0 \uc54a\uc74c<ul><li>undefined \ubcc0\uc218\ub85c \ucde8\uae09<\/li><\/ul><\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nrouter.delete('\/', function(req, res, next) {\n    res.send('Year : ' + req.query.year);\n});\n<\/pre><\/div>\n\n\n<ul><li>DELETE localhost:3000 -&gt; \uc751\ub2f5 : Year : undefined<\/li><li>DELETE localhost:3000\/?year=2021 -&gt; \uc751\ub2f5 : Year : 2021<\/li><\/ul>\n\n\n\n<h3>HTTP Body<\/h3>\n\n\n\n<p>\u2022 POST, PUT \uba54\uc11c\ub4dc\ub294 HTTP \uc694\uccad\uc5d0 Body \uc804\ub2ec \uac00\ub2a5<br>\u2022 Body\uc5d0\ub294 text, json, xml, html, \ud30c\uc77c \ub4f1 \uc5b4\ub290 \uac83\uc774\ub77c\ub3c4 \uc804\uc1a1\ub420 \uc218 \uc788\uc74c<br>\u2022 Body\ub294 Content-type \ud5e4\ub354\uc5d0 \ub530\ub77c \uc801\uc808\ud788 \ud574\uc11d\ub418\uc5b4\uc57c \ud568<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n# index.js\napp.use(express.json());\n\n# router.js\nrouter.post('\/', function(req, res, next) {\n    res.send('\uc81c\ubaa9 : ' + req.body.title);\n});\n\n# insomnia test json\n{\n\t&quot;title&quot; : &quot;\ub3c4\uc11c\uc758 \uc81c\ubaa9\uc785\ub2c8\ub2e4.&quot;\n}\n<\/pre><\/div>\n\n\n<ul><li>POST localhost:3000 -&gt; \uc751\ub2f5 : \ub3c4\uc11c\uc758 \uc81c\ubaa9\uc785\ub2c8\ub2e4.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>RESTful API REST \ub780? Representational State Transfer \uc758 \uc57d\uc790 api \uc5d0 \ub300\ud55c \uc0c1\uc138 \uc2a4\ud399\uc744 \ubcf4\uc9c0 \uc54a\uc544\ub3c4 HTTP\uc694\uccad\uc758 \uc5ec\ub7ec \uc815\ubcf4\ub97c \ud1b5\ud574 API\uac00 \ud558\ub294 \uc77c\uc744 \uc54c \uc218 \uc788\ub3c4\ub85d \uc124\uacc4\ub41c api REST API\uac00 \ud45c\ud604\ud558\ub294 \uac83 \uc790\uc6d0\uacfc \uc790\uc6d0\uc5d0 \ub300\ud55c \ud589\uc704 \uc790\uc6d0 : \uc11c\ubc84\uc5d0 \uc800\uc7a5\ub418\uc5b4 \uc788\ub294 \ub370\uc774\ud130\ub4e4 \uc1fc\ud551\ubab0 \uc11c\ube44\uc2a4\uc5d0\uc11c\uc758 \uc790\uc6d0 : \uc0c1\ud488 \ubaa9\ub85d,\uc8fc\ubb38 \ubaa9\ub85d,\ud68c\uc6d0 \ubaa9\ub85d \ub4f1 \ub3c4\uc11c\uad00\ub9ac \uc11c\ube44\uc2a4\uc5d0\uc11c\uc758 \uc790\uc6d0 : [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[38,37],"tags":[],"_links":{"self":[{"href":"http:\/\/mylittleforest.kr\/wordpress\/wp-json\/wp\/v2\/posts\/1249"}],"collection":[{"href":"http:\/\/mylittleforest.kr\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mylittleforest.kr\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mylittleforest.kr\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/mylittleforest.kr\/wordpress\/wp-json\/wp\/v2\/comments?post=1249"}],"version-history":[{"count":6,"href":"http:\/\/mylittleforest.kr\/wordpress\/wp-json\/wp\/v2\/posts\/1249\/revisions"}],"predecessor-version":[{"id":1261,"href":"http:\/\/mylittleforest.kr\/wordpress\/wp-json\/wp\/v2\/posts\/1249\/revisions\/1261"}],"wp:attachment":[{"href":"http:\/\/mylittleforest.kr\/wordpress\/wp-json\/wp\/v2\/media?parent=1249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mylittleforest.kr\/wordpress\/wp-json\/wp\/v2\/categories?post=1249"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mylittleforest.kr\/wordpress\/wp-json\/wp\/v2\/tags?post=1249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}