{"id":41268,"date":"2024-12-01T08:57:44","date_gmt":"2024-12-01T00:57:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/41268\/"},"modified":"2024-12-01T08:57:44","modified_gmt":"2024-12-01T00:57:44","slug":"%e5%9c%a8-go-%e4%b8%ad%e4%bd%bf%e7%94%a8-http-request-%e5%b0%86-url-%e5%8f%82%e6%95%b0%e8%bd%ac%e6%8d%a2%e4%b8%ba-json","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/41268\/","title":{"rendered":"\u5728 Go \u4e2d\u4f7f\u7528 http.Request \u5c06 URL \u53c2\u6570\u8f6c\u6362\u4e3a JSON"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5728 Go \u4e2d\u4f7f\u7528 http.Request \u5c06 URL \u53c2\u6570\u8f6c\u6362\u4e3a JSON<\/span><\/p>\n<h1>\u5728 Go \u4e2d\u4f7f\u7528 http.Request \u5c06 URL \u53c2\u6570\u8f6c\u6362\u4e3a JSON<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 17:36:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728 Go \u4e2d\u4f7f\u7528 http.Request \u5c06 URL \u53c2\u6570\u8f6c\u6362\u4e3a JSON\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u9a8c\u8bc1\u8bf7\u6c42\u4e2d\u53d1\u51fa\u7684\u53c2\u6570\uff0c\u4f46\u4e0e\u4ee5 json \u53d1\u51fa\u8bf7\u6c42\u65f6\u4e0d\u540c\uff0c\u6211\u65e0\u6cd5\u8f7b\u677e\u5730\u5c06\u67e5\u8be2\u53c2\u6570\u8f6c\u6362\u4e3a\u5176\u5bf9\u5e94\u7684\u7ed3\u6784\u4f53\u3002\u4f7f\u7528\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<p>https:\/\/thedevsaddam.medium.com\/an-easy-way-to-validate-go-request-c15182fd11b1<\/p>\n<pre>type itemsrequest struct {\n    item         string `json:\"item\"`\n}<\/pre>\n<pre>func ValidateItemRequest(r *http.Request, w http.ResponseWriter) map[string]interface{} {\n    var itemRequest ItemsRequest\n    rules := govalidator.MapData{\n        \"item\":        []string{\"numeric\"},\n    }\n\n    opts := govalidator.Options{\n        Request:         r,    \n        Rules:           rules,\n        Data:            &amp;itemRequest ,\n        RequiredDefault: true,\n    }\n    v := govalidator.New(opts)\n    \n    e := v.Validate()\n    \/\/ access itemsRequest.item\n\n}<\/pre>\n<p>\u5982\u679c\u6211\u4f7f\u7528 <code>e.validatejson()<\/code> \u5e76\u901a\u8fc7\u6b63\u6587\u4f20\u9012\u6570\u636e\uff0c\u5219\u6b64\u65b9\u6cd5\u6709\u6548<\/p>\n<p>\u5982\u679c\u6211\u4f7f\u7528 <code>e.validate()<\/code> \u5e76\u901a\u8fc7 url \u53c2\u6570\u4f20\u9012\u6570\u636e\uff0c\u5219\u8fd9\u4e0d\u8d77\u4f5c\u7528\u3002<\/p>\n<p>\u6211\u8ba4\u4e3a\u7ed3\u6784\u4e2d\u7684 <code>json<\/code> \u6807\u8bb0\u662f\u95ee\u9898\u6240\u5728\uff0c\u4f46\u5220\u9664\u5b83\u4f1a\u4ea7\u751f\u76f8\u540c\u7684\u7ed3\u679c\u3002\u6211\u4e0d\u8ba4\u4e3a validate() \u5e94\u8be5\u586b\u5145\u8be5\u7ed3\u6784\uff0c\u4f46\u662f\u6211\u5e94\u8be5\u5982\u4f55\u4f20\u9012 url \u7684\u503c\uff1f<\/p>\n<p>\u6211\u77e5\u9053\u6211\u53ef\u4ee5\u4f7f\u7528 <code>r.url.query().get()<\/code> \u624b\u52a8\u63d0\u53d6\u6bcf\u4e2a\u503c\uff0c\u4f46\u5728\u6211\u521a\u521a\u9a8c\u8bc1\u4e86\u6211\u60f3\u8981\u7684\u6240\u6709\u5185\u5bb9\u540e\uff0c\u8fd9\u4f3c\u4e4e\u975e\u5e38\u591a\u4f59\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p> \u53ef\u4ee5\u4e3a\u60a8\u5b8c\u6210\u7e41\u91cd\u7684\u5de5\u4f5c\uff0c\u5c06 <code>url.values<\/code> \u8f6c\u6362\u4e3a <code>struct<\/code> \u5e76\u8fdb\u884c\u4e00\u4e9b\u67b6\u6784\u9a8c\u8bc1\u3002<\/p>\n<p>\u5b83\u652f\u6301\u7c7b\u4f3c\u4e8e json \u7684\u7ed3\u6784\u5b57\u6bb5\u6807\u7b7e\uff0c\u5e76\u4e14\u4e0e json \u517c\u5bb9\uff0c\u5e76\u4e14\u8fd8\u53ef\u4ee5\u4e0e\u8868\u5355\u6570\u636e\u6216\u591a\u90e8\u5206\u8868\u5355\u6570\u636e\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n<pre>package main\n\nimport(\n   \"github.com\/gorilla\/schema\"\n   \"net\/url\"\n   \"fmt\"\n)\n\n\ntype Person struct {\n    Name  string\n    Phone string\n}\n\nfunc main() {\n    var decoder = schema.NewDecoder()\n    u, err := url.Parse(\"http:\/\/localhost\/?name=daniel&amp;phone=612-555-4321\")\n    if err != nil { panic(err) }\n    var person Person\n    err = decoder.Decode(&amp;person, u.Query())\n    if err != nil {\n        \/\/ Handle error\n    }\n    fmt.Printf(\"%+v\\n\", person)\n}<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5728 Go \u4e2d\u4f7f\u7528 http.Request \u5c06 URL \u53c2\u6570\u8f6c\u6362\u4e3a JSON\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u5728 Go \u4e2d\u4f7f\u7528 http.Request \u5c06 URL \u53c2\u6570\u8f6c\u6362\u4e3a JSON \u5728 Go \u4e2d\u4f7f\u7528 http.Request \u5c06 URL \u53c2\u6570\u8f6c\u6362\u4e3a JSON \u6765\u6e90\uff1astackoverflow 2024-04-19 17:36:34 0\u6d4f\u89c8 \u6536\u85cf \u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300a\u5728 Go \u4e2d\u4f7f\u7528 http.Request \u5c06 URL \u53c2\u6570\u8f6c\u6362\u4e3a JSON\u300b\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~ \u95ee\u9898\u5185\u5bb9 \u6211\u6b63\u5728\u9a8c\u8bc1\u8bf7\u6c42\u4e2d\u53d1\u51fa\u7684\u53c2\u6570\uff0c\u4f46\u4e0e\u4ee5 json \u53d1\u51fa\u8bf7\u6c42\u65f6\u4e0d\u540c\uff0c\u6211\u65e0\u6cd5\u8f7b\u677e\u5730\u5c06\u67e5\u8be2\u53c2\u6570\u8f6c\u6362\u4e3a\u5176\u5bf9\u5e94\u7684\u7ed3\u6784\u4f53\u3002\u4f7f\u7528\u4ee5\u4e0b\u5185\u5bb9\uff1a https:\/\/thedevsaddam.medium.com\/an-easy-way-to-validate-go-request-c15182fd11b1 type itemsrequest struct { item string `json:&#8221;item&#8221;` } func ValidateItemRequest(r *http.Request, w http.ResponseWriter) map[string]interface{} { var [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-41268","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=41268"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41268\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=41268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=41268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=41268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}