{"id":1720,"date":"2020-10-09T03:27:55","date_gmt":"2020-10-08T19:27:55","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=1720"},"modified":"2022-02-16T21:52:07","modified_gmt":"2022-02-16T13:52:07","slug":"iris-form-post-2021ironman","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2020\/10\/09\/iris-form-post-2021ironman\/","title":{"rendered":"Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-iris \u7684 form post"},"content":{"rendered":"<h1>form POST<\/h1>\n<p>\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4ecb\u7d39\u5c11\u4f7f\u7528\u8005\u767c\u51fa\u8acb\u6c42\u7684\u624b\u6bb5\u4e4b\u4e00 query string\uff0c\u4f46\u662fquery string\u6709\u9577\u5ea6\u9650\u5236\uff0c\u800c\u4e14\u5bb9\u6613\u88ab\u6514\u622a\u7121\u96b1\u79c1\u53ef\u8a00\uff0c\u6240\u4ee5\u672c\u7bc7\u8981\u4ecb\u7d39\u4f7f\u7528\u8005\u767c\u51fa\u8acb\u6c42\u7684\u624b\u6bb5\u4e4b\u4e8c\uff0cform POST\u3002<\/p>\n<p><!--more--><\/p>\n<h2>iris\u7684form POST<\/h2>\n<p>\u672c\u7bc7\u4ecb\u7d39iris\u7684 form POST\uff0c\u7576\u7136\u8981\u5148\u7531\u4ec0\u9ebc\u662fform POST\u8b1b\u8d77\uff0c\u518d\u4f86\u624d\u5e36\u5230\u95dc\u65bciris\u7684form POST\u600e\u9ebc\u8655\u7406\uff0c\u6240\u4ee5\u6211\u5011\u5148\u7531\u4ecb\u7d39form POST\u958b\u59cb\u3002<\/p>\n<h3>\u4ec0\u9ebc\u662fform POST<\/h3>\n<p>\u6709\u8208\u8da3\u7684\u8b80\u8005\u5011\u53ef\u4ee5\u5148\u770b\u770b<a href=\"https:\/\/en.m.wikipedia.org\/wiki\/POST_(HTTP)\" title=\"POST Http Wiki\" rel=\"nofollow noopener\" target=\"_blank\">POST Http Wiki<\/a>\uff0c\u9019\u88e1\u53ef\u4ee5\u5206\u6210\u5169\u90e8\u5206\u4f86\u8aaa\u660e\uff0c\u7b2c\u4e00\u500b\u662f<code>POST<\/code>\u65b9\u6cd5\uff0c\u53e6\u4e00\u500b\u662f<code>form POST<\/code>\uff0c\u9019\u7bc7\u4e3b\u8981\u662f\u8aaa\u660e<code>form POST<\/code>\uff0c\u6240\u4ee5\u6211\u5011\u91dd\u5c0d<code>form POST<\/code>\u4f86\u8aaa\u660e\uff0c\u7c21\u55ae\u8aaa\u660e\u9019\u4e3b\u984c\u5c31\u662f\u63d0\u4ea4\u4e00\u500b\u8868\u55ae\uff0c\u6240\u4ee5\u63a5\u4e0b\u4f86\u6211\u5011\u91dd\u5c0diris\u5982\u4f55\u8655\u7406<code>form POST<\/code>\u3002<\/p>\n<h3>\u8655\u7406 iris\u7684form POST<\/h3>\n<p>\u9019\u88e1\u6211\u5011\u4f7f\u7528<a href=\"https:\/\/docs.iris-go.com\/iris\/requests\/form\" title=\"docs iris\" rel=\"nofollow noopener\" target=\"_blank\">docs-iris-go<\/a>\u7684\u4f8b\u5b50\u4f86\u8aaa\u660e\uff0c\u8acb\u5927\u5bb6\u770b\u770b\u4ee5\u4e0b\u5167\u5bb9\uff0c\u9996\u5148\u662f<code>html<\/code>\u7684\u90e8\u5206\u3002<\/p>\n<pre><code class=\"language-html\">&lt;form action=&quot;\/&quot; method=&quot;POST&quot;&gt;\n    &lt;p&gt;Check one or more colors&lt;\/p&gt;\n\n    &lt;label for=&quot;red&quot;&gt;Red&lt;\/label&gt;\n    &lt;!-- name can be &quot;colors&quot; too --&gt;\n    &lt;input type=&quot;checkbox&quot; name=&quot;colors[]&quot; value=&quot;red&quot; id=&quot;red&quot;&gt;\n    &lt;label for=&quot;green&quot;&gt;Green&lt;\/label&gt;\n    &lt;input type=&quot;checkbox&quot; name=&quot;colors[]&quot; value=&quot;green&quot; id=&quot;green&quot;&gt;\n    &lt;label for=&quot;blue&quot;&gt;Blue&lt;\/label&gt;\n    &lt;input type=&quot;checkbox&quot; name=&quot;colors[]&quot; value=&quot;blue&quot; id=&quot;blue&quot;&gt;\n    &lt;input type=&quot;submit&quot;&gt;\n&lt;\/form&gt;<\/code><\/pre>\n<p>\u4e0a\u9762\u662f\u4e00\u500b\u7c21\u55ae\u7684<code>html<\/code>\u7684\u8868\u55ae\uff0c\u7576\u6309\u4e0b<code>submit<\/code>\u5f8c\u9001\u51fa\u8868\u55ae\uff0c\u63a5\u4e0b\u4f86\u770b\u770b\u4e0b\u5217\u4f8b\u5b50\u4f86\u5b78\u7fd2\u5982\u4f55\u8655\u7406<code>form POST<\/code><\/p>\n<pre><code class=\"language-go\">package main\n\nimport &quot;github.com\/kataras\/iris\/v12&quot;\n\nfunc main() {\n    app := iris.New()\n    app.RegisterView(iris.HTML(&quot;.\/templates&quot;, &quot;.html&quot;))\n\n    app.Get(&quot;\/&quot;, showForm)\n    app.Post(&quot;\/&quot;, handleForm)\n\n    app.Listen(&quot;:8080&quot;)\n}\n\nfunc showForm(ctx iris.Context) {\n    ctx.View(&quot;form.html&quot;)\n}\n\ntype formExample struct {\n    Colors []string `form:&quot;colors[]&quot;` \/\/ or just &quot;colors&quot;.\n}\n\nfunc handleForm(ctx iris.Context) {\n    var form formExample\n    err := ctx.ReadForm(&amp;form)\n    if err != nil {\n        ctx.StopWithError(iris.StatusBadRequest, err)\n        return\n    }\n\n    ctx.JSON(iris.Map{&quot;Colors&quot;: form.Colors})\n}<\/code><\/pre>\n<p>\u4e2d\u9019\u500b\u4f8b\u5b50\u505a\u4e86\u5169\u4ef6\u4e8b\uff0c\u5176\u4e2d\u4e4b\u4e00\u662f\u4e4b\u524d\u63d0\u904e\u7684\u7d81\u5b9a<code>view<\/code>\uff0c\u9019\u88e1\u7d81\u5b9a\u7684\u5167\u5bb9\u5c31\u662f\u524d\u9762\u7684<code>html<\/code>\u7684\u4f8b\u5b50\uff0c\u53e6\u5916\u4e00\u4ef6\u4e8b\u5c31\u662f\u7576\u6536\u5230\u4f7f\u7528\u8005\u63d0\u4ea4\u7684\u8868\u55ae\u8981\u5982\u4f55\u8655\u7406\u3002<\/p>\n<p>\u9019\u88e1\u7684\u8655\u7406\u65b9\u5f0f\u5c31\u662f\u5b9a\u7fa9\u4e00\u500bmodel\u7136\u5f8c\u547c\u53eb<code>ReadForm<\/code>\u7684\u65b9\u6cd5\u4f86\u5c07\u63d0\u4ea4\u7684\u8868\u55ae\u7d81\u5b9a\u6210\u6211\u5011\u6307\u5b9a\u7684model\uff0c\u5982\u6b64\u6211\u5011\u5c31\u53ef\u4ee5\u91dd\u5c0dmodel\u4f86\u64cd\u4f5c\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u672c\u7bc7\u6587\u7ae0\u4ecb\u7d39\u4e86\u5982\u4f55\u5728iris\u5167\u4f7f\u7528model binding\u7684\u65b9\u5f0f\u4f86\u8655\u7406form POST\uff0c\u9664\u4e86\u8868\u55ae\u5916form POST\u9084\u53ef\u4ee5\u50b3\u905e\u6a94\u6848\uff0c\u5f85\u4e0b\u4e00\u7bc7\u518d\u8ddf\u5927\u5bb6\u4ecb\u7d39\u8a72\u5982\u4f55\u8655\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>form POST \u4e0a\u4e00\u7bc7\u6587\u7ae0\u4ecb\u7d39\u5c11\u4f7f\u7528\u8005\u767c\u51fa\u8acb\u6c42\u7684\u624b\u6bb5\u4e4b\u4e00 query string\uff0c\u4f46\u662fquery str &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2020\/10\/09\/iris-form-post-2021ironman\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-iris \u7684 form post\u3009<\/span><\/a><\/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,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[158,81,84],"class_list":["post-1720","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2020ironman","tag-go","tag-iris"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=1720"}],"version-history":[{"count":15,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1720\/revisions"}],"predecessor-version":[{"id":2955,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1720\/revisions\/2955"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=1720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=1720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=1720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}