{"id":27,"date":"2022-02-04T00:32:37","date_gmt":"2022-02-04T00:32:37","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/yangellen\/?p=27"},"modified":"2022-02-06T00:52:34","modified_gmt":"2022-02-06T00:52:34","slug":"flutter-http-post-request","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/yangellen\/2022\/02\/04\/flutter-http-post-request\/","title":{"rendered":"Flutter Http POST request"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5184\/files\/2022\/02\/hamed-daram-5fbmfaInwg-unsplash-1024x683.jpg\" alt=\"\" class=\"wp-image-28\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5184\/files\/2022\/02\/hamed-daram-5fbmfaInwg-unsplash-1024x683.jpg 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5184\/files\/2022\/02\/hamed-daram-5fbmfaInwg-unsplash-300x200.jpg 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5184\/files\/2022\/02\/hamed-daram-5fbmfaInwg-unsplash-768x512.jpg 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5184\/files\/2022\/02\/hamed-daram-5fbmfaInwg-unsplash-1536x1024.jpg 1536w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5184\/files\/2022\/02\/hamed-daram-5fbmfaInwg-unsplash-2048x1366.jpg 2048w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5184\/files\/2022\/02\/hamed-daram-5fbmfaInwg-unsplash-1568x1046.jpg 1568w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"so-i-have-created-a-form-for-my-project-how-do-i-send-the-user-input-to-the-database\">So, I have created a form for my project, how do I send the user input to the database?<\/h2>\n\n\n\n<p>When the form only has one or two questions such as login form, it&#8217;s easy to transfer user input. However, many forms have multiple questions. It will be a lot easier to transfer information with a data transfer object. <\/p>\n\n\n\n<p>Let&#8217;s have simple class to see how this work,<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><code>class<\/code> User {<\/p><p>&nbsp; String userName;<\/p><p>&nbsp; String address;<\/p><p>&nbsp; String zipCode;<\/p><p>&nbsp; String city;<\/p><p>&nbsp; String state;<\/p><p>User (this.userName, this.address, this.zipCode , this.city, this.state}<\/p><\/blockquote>\n<\/div><\/div>\n\n\n\n<p>With the User class, you can create a user object and whenever user enter the information, you can store with it. <\/p>\n\n\n\n<p><strong>final newUser = User( );<\/strong>   <\/p>\n\n\n\n<p>This newUser will be your data transfer object to store information. For example, <\/p>\n\n\n\n<p>newUser.userName = &#8220;Emily K<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-next\">What next?<\/h2>\n\n\n\n<p>This depends on how you are going to store your data in database. Here I&#8217;m going to send my data to a POST api since this is what we are doing with our project. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"convert-object-to-json\">Convert Object to JSON<\/h2>\n\n\n\n<p>Before I can transfer the information from my object, I need to convert it to JSON. You can use <strong>dart:convert<\/strong> libray&#8217;s built in <strong>jsonEncode() <\/strong>function to convert user object to JSON. But if you call jsonEncode() without creating<strong> toJson() <\/strong>method, you will get an error. Remember the User class we created earlier, I want you to add toJson() method there.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>class User {<\/p><p>&nbsp; &#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;<\/p><p>&nbsp; Map toJson() =&gt; {<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;userName&#8221;: userName,<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;address&#8221;: address,<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;zipCode&#8221;: zipCode,<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;city&#8221;: city,<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8220;state&#8221;: state,<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<\/p><p>}<\/p><p><\/p><cite>Now you are ready to convert your object to JSON. In the you are converting the object,<strong> import &#8216;dart: convert&#8217;;<\/strong> Assuming you already store all information for newUser object, here is how you can use jsonEncode():<\/cite><\/blockquote>\n\n\n\n<p><strong>String jsonUser = jsonEncode(newUser);<\/strong><\/p>\n\n\n\n<p>You don&#8217;t need to call toJson( ) here because jsonEncode will call toJson( ) for you. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"send-data-to-the-internet\">Send data to the internet<\/h2>\n\n\n\n<p>In Flutter documentation, you can follow <a href=\"https:\/\/docs.flutter.dev\/cookbook\/networking\/send-data\">https:\/\/docs.flutter.dev\/cookbook\/networking\/send-data<\/a> using http package to send your data to an API. Their example use {JSON} placeholder, free fake API for testing and prototyping. This method will work for most API but it did not work for the API we created.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-happen\">What happen???<\/h2>\n\n\n\n<p>My post request was rejected because my request body wasn&#8217;t JSON. What??? Didn&#8217;t I just change my object to JSON in the above steps? I verified my jsonUser online, it is a valid JSON and I have specified headers: {&#8220;Content-Type&#8221;:&#8221;application\/json&#8221;}.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-they-didn-t-tell-you\">What they didn&#8217;t tell you?<\/h2>\n\n\n\n<p>It turns out when you use the http package, it changes the request body to  <strong>application\/json ; charset=utf-8<\/strong>. The server doesn&#8217;t expect to see the suffix so it&#8217;s being rejected. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"now-what\">Now what?<\/h2>\n\n\n\n<p>You can contact the server to have the API accept &#8220;<strong>application\/json ; charset=utf-8<\/strong>&#8221; or you can use <strong>HttpClient<\/strong>.<\/p>\n\n\n\n<p>In you file,<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>import &#8216;dart:convert&#8217;;<\/p><p>import &#8216;dart:io&#8217;;<\/p><p>import &#8216;dart:async&#8217;;<\/p><cite>Example code: <\/cite><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>&nbsp; Future&lt;void&gt; submitPostForm() async {<\/p><p>&nbsp;&nbsp;&nbsp; String Url = &#8216;https:\/\/someapi.wn.r.appspot.com\/&#8217;<\/p><p>&nbsp;&nbsp;&nbsp; HttpClient httpClient = HttpClient();<\/p><p>&nbsp;&nbsp;&nbsp; HttpClientRequest request = await    httpClient.postUrl(Uri.parse(url));<\/p><p>&nbsp;&nbsp;&nbsp; request.headers.set(&#8216;content-type&#8217;, &#8216;application\/json&#8217;);<\/p><p>&nbsp;&nbsp;&nbsp; request.add(utf8.encode(jsonEncode(newUser)));<\/p><p>&nbsp;&nbsp;&nbsp; HttpClientResponse response = await request.close();<\/p><p>&nbsp;..codes to handle response<\/p><p>&nbsp; }<\/p><p><\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"tada-201-created\">Tada!!! 201 Created!<\/h2>\n\n\n\n<figure class=\"wp-block-image alignwide size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"683\" height=\"1024\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5184\/files\/2022\/02\/brooke-cagle-oTweoxMKdkA-unsplash-683x1024.jpg\" alt=\"\" class=\"wp-image-29\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5184\/files\/2022\/02\/brooke-cagle-oTweoxMKdkA-unsplash-683x1024.jpg 683w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5184\/files\/2022\/02\/brooke-cagle-oTweoxMKdkA-unsplash-200x300.jpg 200w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5184\/files\/2022\/02\/brooke-cagle-oTweoxMKdkA-unsplash-768x1152.jpg 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5184\/files\/2022\/02\/brooke-cagle-oTweoxMKdkA-unsplash-1024x1536.jpg 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5184\/files\/2022\/02\/brooke-cagle-oTweoxMKdkA-unsplash-1365x2048.jpg 1365w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5184\/files\/2022\/02\/brooke-cagle-oTweoxMKdkA-unsplash-1568x2352.jpg 1568w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/5184\/files\/2022\/02\/brooke-cagle-oTweoxMKdkA-unsplash-scaled.jpg 1707w\" sizes=\"auto, (max-width: 683px) 100vw, 683px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>So, I have created a form for my project, how do I send the user input to the database? When the form only has one or two questions such as login form, it&#8217;s easy to transfer user input. However, many forms have multiple questions. It will be a lot easier to transfer information with a&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.oregonstate.edu\/yangellen\/2022\/02\/04\/flutter-http-post-request\/\">Continue reading <span class=\"screen-reader-text\">Flutter Http POST request<\/span><\/a><\/p>\n","protected":false},"author":12023,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-27","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/yangellen\/wp-json\/wp\/v2\/posts\/27","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/yangellen\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/yangellen\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/yangellen\/wp-json\/wp\/v2\/users\/12023"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/yangellen\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":4,"href":"https:\/\/blogs.oregonstate.edu\/yangellen\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":34,"href":"https:\/\/blogs.oregonstate.edu\/yangellen\/wp-json\/wp\/v2\/posts\/27\/revisions\/34"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/yangellen\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/yangellen\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/yangellen\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}