{"id":3884,"date":"2024-04-08T13:38:08","date_gmt":"2024-04-08T13:38:08","guid":{"rendered":"http:\/\/van-maanen.com\/?p=3883"},"modified":"2024-04-08T13:38:08","modified_gmt":"2024-04-08T13:38:08","slug":"use-the-node-js-server-as-restful-app-server","status":"publish","type":"post","link":"http:\/\/van-maanen.com\/?p=3884","title":{"rendered":"Use the node.js server as restful app server"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">One may use the node.js server as a means to get acquainted with restful services. Let us assume this server is installed and started with<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">json-server \"C:\\Users\\tomva\\SynologyDrive\\python\\json server\\db.json\" --port 8000\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can then see the json database within the browser &#8220;http:\/\/localhost:8000\/extra&#8221;.  We may add some information by:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import requests\nimport json\napi_url = \"http:\/\/localhost:8000\/extra\"\ntodo = {\"userId\": 1, \"title\": \"Buy nore sugar\", \"completed\": False}\nheaders =  {\"Content-Type\":\"application\/json\"}\nresponse = requests.post(api_url, data=json.dumps(todo), headers=headers)\nprint(response.json())\nprint(response.status_code)\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Data is added with &#8216;id&#8217;: &#8216;8701&#8217;. This id is important if one would like to change some data.  Look at the put methode that is used below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\nimport requests\nimport json\n\nurl = 'http:\/\/localhost:8000\/extra\/8701'\ndata = {\"id\": 3,\"userId\": 2, \"title\": \"drink beer\",  \"body\": \"drinking beer is nice\"}\nheaders = {'Content-Type':'application\/json; charset=UTF-8'}\nresponse = requests.put(url, data=json.dumps(data), headers=headers)\n\nprint(response.status_code)\nprint(response.text)\n\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The data can be retrieved with the get method:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import requests\napi_url = \"http:\/\/localhost:8000\/extra\/8701\"\nresponse = requests.get(api_url)\nresponse.json()\nprint(response.text)\nprint(response.status_code)\n\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The response.text contains the data:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"http:\/\/van-maanen.com\/wp-content\/uploads\/2024\/04\/Screenshot-2024-04-08-160256.png\" alt=\"\" class=\"wp-image-3886\"\/><figcaption class=\"wp-element-caption\">Finally, we may delete this entry by:<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">import requests\nimport json\n\nurl = 'http:\/\/localhost:8000\/extra\/8701'\nheaders = {'Content-Type': 'application\/json; charset=UTF-8'}\nresponse = requests.delete(url, headers=headers)\nprint(response.status_code) # 200\nprint(response.ok) # True<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>One may use the node.js server as a means to get acquainted with restful services. Let us assume this server is installed and started with json-server &#8220;C:\\Users\\tomva\\SynologyDrive\\python\\json server\\db.json&#8221; &#8211;port 8000 We can then see the json database within the browser &#8220;http:\/\/localhost:8000\/extra&#8221;. We may add some information by: import requests import json api_url = &#8220;http:\/\/localhost:8000\/extra&#8221; todo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-3884","post","type-post","status-publish","format-standard","hentry","category-niet-gecategoriseerd"],"_links":{"self":[{"href":"http:\/\/van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/van-maanen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/van-maanen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3884"}],"version-history":[{"count":0,"href":"http:\/\/van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3884\/revisions"}],"wp:attachment":[{"href":"http:\/\/van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3884"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}