{"id":12190,"date":"2019-08-06T17:03:00","date_gmt":"2019-08-06T15:03:00","guid":{"rendered":"https:\/\/pi3g.com\/?p=12190"},"modified":"2019-08-06T17:03:00","modified_gmt":"2019-08-06T15:03:00","slug":"getting-post-requests-to-work-with-axios-kemal-crystal-lang-vue-js","status":"publish","type":"post","link":"https:\/\/pi3g.com\/de\/getting-post-requests-to-work-with-axios-kemal-crystal-lang-vue-js\/","title":{"rendered":"POST-Anfragen mit Axios, Kemal (crystal lang), vue.js zum Laufen bringen"},"content":{"rendered":"<p>In picockpit.com, I am using AJAX requests to communicate with the backend for update operations. For instance,<\/p>\n<p><a href=\"https:\/\/pi3g.com\/wp-content\/uploads\/2019\/08\/image.png\"><img loading=\"lazy\" decoding=\"async\" width=\"534\" height=\"139\" title=\"image\" style=\"display: inline; background-image: none;\" alt=\"image\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2019\/08\/image_thumb.png\" border=\"0\"><\/a><\/p>\n<p>if you want to change the name of this Pi, you would select:<\/p>\n<p><a href=\"https:\/\/pi3g.com\/wp-content\/uploads\/2019\/08\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"541\" height=\"161\" title=\"image\" style=\"display: inline; background-image: none;\" alt=\"image\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2019\/08\/image_thumb-1.png\" border=\"0\"><\/a><\/p>\n<p>which shows an inline form:<\/p>\n<p><a href=\"https:\/\/pi3g.com\/wp-content\/uploads\/2019\/08\/image-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"545\" height=\"130\" title=\"image\" style=\"display: inline; background-image: none;\" alt=\"image\" src=\"https:\/\/pi3g.com\/wp-content\/uploads\/2019\/08\/image_thumb-2.png\" border=\"0\"><\/a><\/p>\n<p>how to show this inline form with vue.js is besides the point of this post (even though very interesting!)<\/p>\n<p>Clicking on Cancel will cancel the renaming, clicking on OK will initiate an AXIOS request, which will talk to the backend. <\/p>\n<p>Here\u2019s the JS code:<\/p>\n<blockquote>\n<p>commitEdit: function (event) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp; let name = this.name;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; let serial = this.serial;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; let pointer = this;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; this.renameSpinnerMode = true;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; axios.post(&#8216;\/mypis\/&#8217; + serial + &#8216;\/changename&#8217;,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name: name<br \/>&nbsp;&nbsp;&nbsp;&nbsp; })<br \/>&nbsp;&nbsp;&nbsp;&nbsp; .then(function (response) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pointer.renameMode = false;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pointer.renameSpinnerMode = false;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pointer.backupName = pointer.name;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; })<br \/>&nbsp;&nbsp;&nbsp;&nbsp; .catch(function (error) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; info(&#8220;Sorry, name of &#8221; + &#8216;&#8221;&#8216; + pointer.backupName&nbsp; + &#8216;&#8221;&#8216; + &#8221; could not be changed. &#8221; + error );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pointer.name = pointer.backupName;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pointer.renameMode = false;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pointer.renameSpinnerMode = false;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });<br \/>\n}<\/p>\n<p>\n<\/p>\n<\/blockquote>\n<p>As you can see, I am using the default axios syntax which is suggested.<\/p>\n<p>Furthermore, I am passing in a pointer (\u201cpointer\u201d) to \u201cthis\u201d (this referring to the Vue.js component!), so that I can modify the component\u2019s state in the callbacks from Axios.<\/p>\n<p>In the Kemal powered backend, I have this code:<\/p>\n<blockquote>\n<p>post &#8220;\/mypis\/:serial\/changename&#8221; do |env|<br \/>&nbsp;&nbsp;&nbsp;&nbsp; returncode = 400<br \/>&nbsp;&nbsp;&nbsp;&nbsp; (\u2026)<br \/>&nbsp;&nbsp;&nbsp;&nbsp; newname = &#8220;&#8221;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; serial = &#8220;&#8221;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp; if env.params.url.has_key?(&#8220;serial&#8221;)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; serial = env.params.url[&#8220;serial&#8221;].not_nil! if !env.params.url[&#8220;serial&#8221;].nil?<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; json_raw = env.request.body.as(IO).gets_to_end<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data = Hash(String, String).from_json(json_raw)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if data.has_key?(&#8220;name&#8221;)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newname = data[&#8220;name&#8221;].not_nil!<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end<br \/>&nbsp;&nbsp;&nbsp;&nbsp; end<\/p>\n<p><\/p>\n<p>(\u2026)<\/p>\n<p>end<\/p>\n<\/blockquote>\n<p><strong>As you can see, we are actually receiving the data JSON encoded!<\/strong><\/p>\n<p>That was the big problem before, I was trying to receive the data form-encoded (env.params.body[\u201cname\u201d]) in Kemal, and not JSON encoded! <\/p>\n<p>In case the data is JSON encoded, env.params.body will be empty!<\/p>\n<p>It is much easier to switch the backend implementation to handle JSON, than to try and convince AXIOS to encode the data as a form.<\/p>\n<h1>Axios \u201cparams\u201d solution<\/h1>\n<p>By the&nbsp; way, often on the web you will find the suggestion to set the params key for Axios. This is bad in my opinion, as it will actually encode the data as part of the URL.<\/p>\n<p>You would have to rewrite the URL to get the query string, and interpret the data in Kemal. <\/p>\n<p>And: the URL is subject to a lenght limitation. <\/p>\n<p>Therefore, with POST requests, the parsing JSON route is the more solid one, IMHO.<\/p>\n<h1>We are for hire \u2013 consulting, development, project management \u2026 <\/h1>\n<p>Daily rate: 1500 \u20ac net. If you are looking for a flexible developer with a focus on the Raspberry Pi, we might be a good fit for you.<\/p>\n<p><a href=\"https:\/\/buyzero.de\/kontakt\" target=\"_blank\">Contact us.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In picockpit.com verwende ich AJAX-Anfragen zur Kommunikation mit dem Backend f\u00fcr Aktualisierungsvorg\u00e4nge. Wenn Sie z. B. den Namen dieses Pi \u00e4ndern m\u00f6chten, w\u00e4hlen Sie:, was ein Inline-Formular anzeigt: Wie man dieses Inline-Formular mit vue.js anzeigt, ist nicht Gegenstand dieses Beitrags (auch wenn es sehr interessant ist!)...<\/p>","protected":false},"author":830,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[416,666],"tags":[668,409,671,670,495,669,667],"class_list":["post-12190","post","type-post","status-publish","format-standard","hentry","category-crystal-language","category-vue-js","tag-axios","tag-crystal","tag-form-encoded","tag-json","tag-kemal","tag-post","tag-vue"],"_links":{"self":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/12190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/users\/830"}],"replies":[{"embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/comments?post=12190"}],"version-history":[{"count":1,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/12190\/revisions"}],"predecessor-version":[{"id":12191,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/12190\/revisions\/12191"}],"wp:attachment":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/media?parent=12190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/categories?post=12190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/tags?post=12190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}