{"id":205947,"date":"2025-06-12T11:15:00","date_gmt":"2025-06-12T03:15:00","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205947\/"},"modified":"2025-06-12T11:15:00","modified_gmt":"2025-06-12T03:15:00","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8%e4%b8%8d%e4%bd%bf%e7%94%a8-json-%e7%9a%84%e6%83%85%e5%86%b5%e4%b8%8b%e8%a7%a3%e6%9e%90-http-%e8%af%b7%e6%b1%82%e4%b8%bb%e4%bd%93%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205947\/","title":{"rendered":"\u5982\u4f55\u5728\u4e0d\u4f7f\u7528 JSON \u7684\u60c5\u51b5\u4e0b\u89e3\u6790 HTTP \u8bf7\u6c42\u4e3b\u4f53\uff1f"},"content":{"rendered":"<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728GOLANG\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u5728\u4e0d\u4f7f\u7528 JSON \u7684\u60c5\u51b5\u4e0b\u89e3\u6790 HTTP \u8bf7\u6c42\u4e3b\u4f53\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241029\/1730160588672027cc585fc.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u5982\u4f55\u89e3\u6790\u975e json \u683c\u5f0f\u7684 http \u8bf7\u6c42 body<\/strong>,\u5728\u5904\u7406 http \u8bf7\u6c42\u65f6\uff0c\u89e3\u6790\u8bf7\u6c42 body \u4e2d\u7684\u5185\u5bb9\u81f3\u5173\u91cd\u8981\u3002go \u8bed\u8a00\u4e3a\u89e3\u6790 json \u683c\u5f0f\u7684 body \u63d0\u4f9b\u4e86\u4fbf\u5229\u7684\u65b9\u6cd5\uff0c\u4f46\u5bf9\u4e8e\u5176\u4ed6\u683c\u5f0f\u7684\u5185\u5bb9\uff0c\u5219\u9700\u8981\u91c7\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u3002,\u672c\u6587\u5c06\u63a2\u8ba8\u5982\u4f55\u89e3\u6790\u975e json \u683c\u5f0f\u7684 http \u8bf7\u6c42 body\uff0c\u5e76\u63d0\u4f9b\u4e00\u79cd\u65e0\u9700\u7f16\u5199\u81ea\u5b9a\u4e49\u4ee3\u7801\u7684\u73b0\u6709\u89e3\u51b3\u65b9\u6848\u3002,\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a,\u8fd9\u4e2a\u4ee3\u7801\u6bb5\u5c06\u8bfb\u53d6\u8bf7\u6c42 body \u7684\u5185\u5bb9\u5e76\u5c06\u5176\u6253\u5370\u5728\u63a7\u5236\u53f0\u4e0a\u3002\u7136\u800c\uff0c\u5b83\u65e0\u6cd5\u6b63\u786e\u89e3\u6790 body \u4e2d\u7684\u5185\u5bb9\uff0c\u56e0\u4e3a body \u7684\u683c\u5f0f\u5e76\u975e json\u3002,<br \/>\n<strong>\u4f7f\u7528 r.postformvalue() \u548c r.formfile()<\/strong>,go \u8bed\u8a00\u4e3a\u89e3\u6790 multipart\/form-data \u683c\u5f0f\u7684 body \u63d0\u4f9b\u4e86 r.postformvalue() \u548c r.formfile() \u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u8868\u5355\u5b57\u6bb5\u7684\u540d\u79f0\u63d0\u53d6\u5b57\u7b26\u4e32\u503c\u6216\u6587\u4ef6\u3002,\u4ee5\u4e0b\u4ee3\u7801\u6bb5\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u89e3\u6790 body\uff1a,\u8fd9\u6837\uff0c\u5c31\u53ef\u4ee5\u6839\u636e\u8868\u5355\u5b57\u6bb5\u7684\u540d\u79f0\u63d0\u53d6\u5355\u72ec\u7684\u503c\u6216\u6587\u4ef6\u5185\u5bb9\u3002,<br \/>\n<strong>\u73b0\u6210\u89e3\u51b3\u65b9\u6848<\/strong>,\u5982\u679c\u4e0d\u60f3\u81ea\u5df1\u7f16\u5199\u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5305\u6765\u89e3\u6790\u975e json \u683c\u5f0f\u7684 body \u5185\u5bb9\u3002\u4f8b\u5982\uff0cgithub.com\/valyala\/fasthttp \u5305\u63d0\u4f9b\u4e86 request.multipartform() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u89e3\u6790 multipart\/form-data \u683c\u5f0f\u7684 body\u3002,\u8981\u4f7f\u7528 fasthttp \u5305\u89e3\u6790 body\uff0c\u9700\u8981\u5148\u5bfc\u5165\u5b83\uff0c\u7136\u540e\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a,fasthttp \u5305\u7684 multipartform() \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a<br \/>\n<em>multipart\/form-data<\/em> \u89e3\u6790\u5668\uff0c\u8be5\u89e3\u6790\u5668\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u83b7\u53d6\u8868\u5355\u5b57\u6bb5\u7684\u503c\u6216\u6587\u4ef6\u3002\u5b83\u662f\u4e00\u4e2a\u9ad8\u6548\u4e14\u53ef\u9760\u7684\u7b2c\u4e09\u65b9\u89e3\u51b3\u65b9\u6848\u3002,\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301GOLANG\uff01\u66f4\u591a\u5173\u4e8eGOLANG\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u3002,\u5982\u4f55\u89e3\u6790\u975e json \u683c\u5f0f\u7684 http \u8bf7\u6c42 body,\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728GOLANG\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u5728\u4e0d\u4f7f\u7528 JSON \u7684\u60c5\u51b5\u4e0b\u89e3\u6790 HTTP \u8bf7\u6c42\u4e3b\u4f53\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5982\u4f55\u5728\u4e0d\u4f7f\u7528 JSON \u7684\u60c5\u51b5\u4e0b\u89e3\u6790 HTTP \u8bf7\u6c42\u4e3b\u4f53\uff1f<\/span><\/p>\n<h1>\u5982\u4f55\u5728\u4e0d\u4f7f\u7528 JSON \u7684\u60c5\u51b5\u4e0b\u89e3\u6790 HTTP \u8bf7\u6c42\u4e3b\u4f53\uff1f<\/h1>\n<p><span>\u6765\u6e90\uff1aphp<\/span><br \/>\n<span>2024-10-29 08:10:04<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728GOLANG\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u5728\u4e0d\u4f7f\u7528 JSON \u7684\u60c5\u51b5\u4e0b\u89e3\u6790 HTTP \u8bf7\u6c42\u4e3b\u4f53\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241029\/1730160588672027cc585fc.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u89e3\u6790\u975e json \u683c\u5f0f\u7684 http \u8bf7\u6c42 body<\/strong><\/p>\n<p>\u5728\u5904\u7406 http \u8bf7\u6c42\u65f6\uff0c\u89e3\u6790\u8bf7\u6c42 body \u4e2d\u7684\u5185\u5bb9\u81f3\u5173\u91cd\u8981\u3002go \u8bed\u8a00\u4e3a\u89e3\u6790 json \u683c\u5f0f\u7684 body \u63d0\u4f9b\u4e86\u4fbf\u5229\u7684\u65b9\u6cd5\uff0c\u4f46\u5bf9\u4e8e\u5176\u4ed6\u683c\u5f0f\u7684\u5185\u5bb9\uff0c\u5219\u9700\u8981\u91c7\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u672c\u6587\u5c06\u63a2\u8ba8\u5982\u4f55\u89e3\u6790\u975e json \u683c\u5f0f\u7684 http \u8bf7\u6c42 body\uff0c\u5e76\u63d0\u4f9b\u4e00\u79cd\u65e0\u9700\u7f16\u5199\u81ea\u5b9a\u4e49\u4ee3\u7801\u7684\u73b0\u6709\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>func sayhelloname(w http.responsewriter, r *http.request) {\n    fmt.println(\"header=\", r.header)\n\n    s, _ := ioutil.readall(r.body)\n    fmt.printf(\"body=%s\\n\", s)\n}<\/pre>\n<p>\u8fd9\u4e2a\u4ee3\u7801\u6bb5\u5c06\u8bfb\u53d6\u8bf7\u6c42 body \u7684\u5185\u5bb9\u5e76\u5c06\u5176\u6253\u5370\u5728\u63a7\u5236\u53f0\u4e0a\u3002\u7136\u800c\uff0c\u5b83\u65e0\u6cd5\u6b63\u786e\u89e3\u6790 body \u4e2d\u7684\u5185\u5bb9\uff0c\u56e0\u4e3a body \u7684\u683c\u5f0f\u5e76\u975e json\u3002<\/p>\n<p><strong>\u4f7f\u7528 r.postformvalue() \u548c r.formfile()<\/strong><\/p>\n<p>go \u8bed\u8a00\u4e3a\u89e3\u6790 multipart\/form-data \u683c\u5f0f\u7684 body \u63d0\u4f9b\u4e86 r.postformvalue() \u548c r.formfile() \u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u8868\u5355\u5b57\u6bb5\u7684\u540d\u79f0\u63d0\u53d6\u5b57\u7b26\u4e32\u503c\u6216\u6587\u4ef6\u3002<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u6bb5\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u89e3\u6790 body\uff1a<\/p>\n<pre>fmt.printf(\"postformvalue(smallfilemd5)=%s\\n\", r.postformvalue(\"smallfilemd5\"))\nfmt.printf(\"postformvalue(deletefile)=%s\\n\", r.postformvalue(\"deletefile\"))\n\nfile, _, err := r.formfile(\"projectuploader\")\nif err != nil {\n    fmt.println(err)\n}\ndefer file.close()\ns1, _ := ioutil.readall(file)\nfmt.printf(\"filecontent=%s\\n\", s1)<\/pre>\n<p>\u8fd9\u6837\uff0c\u5c31\u53ef\u4ee5\u6839\u636e\u8868\u5355\u5b57\u6bb5\u7684\u540d\u79f0\u63d0\u53d6\u5355\u72ec\u7684\u503c\u6216\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<p><strong>\u73b0\u6210\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u5982\u679c\u4e0d\u60f3\u81ea\u5df1\u7f16\u5199\u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5305\u6765\u89e3\u6790\u975e json \u683c\u5f0f\u7684 body \u5185\u5bb9\u3002\u4f8b\u5982\uff0cgithub.com\/valyala\/fasthttp \u5305\u63d0\u4f9b\u4e86 request.multipartform() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u89e3\u6790 multipart\/form-data \u683c\u5f0f\u7684 body\u3002<\/p>\n<p>\u8981\u4f7f\u7528 fasthttp \u5305\u89e3\u6790 body\uff0c\u9700\u8981\u5148\u5bfc\u5165\u5b83\uff0c\u7136\u540e\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>import \"github.com\/valyala\/fasthttp\"\n\nfunc main() {\n    fmt.Printf(\"PostFormValue(smallFileMd5)=%s\\n\", r.PostFormValue(\"smallFileMd5\"))\n    fmt.Printf(\"PostFormValue(deleteFile)=%s\\n\", r.PostFormValue(\"deleteFile\"))\n\n    file, _, err := r.FormFile(\"projectUploader\")\n    if err != nil {\n        fmt.Println(err)\n    }\n    defer file.Close()\n    s1, _ := ioutil.ReadAll(file)\n    fmt.Printf(\"filecontent=%s\\n\", s1)\n}<\/pre>\n<p>fasthttp \u5305\u7684 multipartform() \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a <em>multipart\/form-data<\/em> \u89e3\u6790\u5668\uff0c\u8be5\u89e3\u6790\u5668\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u83b7\u53d6\u8868\u5355\u5b57\u6bb5\u7684\u503c\u6216\u6587\u4ef6\u3002\u5b83\u662f\u4e00\u4e2a\u9ad8\u6548\u4e14\u53ef\u9760\u7684\u7b2c\u4e09\u65b9\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301GOLANG\uff01\u66f4\u591a\u5173\u4e8eGOLANG\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u3002<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1aphp \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664  <\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5bcc\u53ef\u89c6\u2014\u2014\u98a0\u8986\u4f20\u7edf\u7684\u5e73\u677f\u7535\u8111\u4f53\u9a8c\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   2024\u8fdb\u535a\u4f1a\u65b0\u601d\u79d1\u6280\u5c55\u53f0\u63d0\u524d\u63ed\u79d8\uff0c\u4ee5\u521b\u65b0\u70b9\u4eae\u201c\u82af\u201d\u8d28\u672a\u6765\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728G&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205947","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205947"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205947\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}