{"id":128926,"date":"2018-11-30T10:30:58","date_gmt":"2018-11-30T02:30:58","guid":{"rendered":"https:\/\/gulass.cn\/?p=128926"},"modified":"2018-11-26T10:34:58","modified_gmt":"2018-11-26T02:34:58","slug":"golang-post-get","status":"publish","type":"post","link":"https:\/\/gulass.cn\/golang-post-get.html","title":{"rendered":"golang\u4e0b\u5904\u7406post\u548cget\u6570\u636e\u7684\u65b9\u6cd5"},"content":{"rendered":"

\u4e4b\u524d\u4f7f\u7528python\u8fdb\u884c\u7f16\u7a0b\u7684\u65f6\u5019\uff0c\u6700\u5e38\u7528\u7684\u5c31\u662f\u901a\u8fc7post\u548cget\u4e00\u4e2aURL\u6293\u53d6\u6240\u9700\u7684\u6570\u636e\uff0c\u4e4b\u524d\u6709\u4e00\u4e2a\u77ed\u4fe1\u63a5\u53e3\u4f7f\u7528\u7684python\u5b9e\u73b0\u7684\uff08post\u6570\u636e\u5230\u67d0\u4e00\u7f51\u5173URL\uff09\uff0c\u4f46\u7531\u4e8epython\u6e90\u7801\u90fd\u662f\u516c\u5f00\u7684\uff08pyc\u4e5f\u5f88\u5bb9\u6613\u5c31\u53cd\u7f16\u8bd1\u51fa\u6765\uff09\uff0c\u6240\u4ee5\u51c6\u5907\u4f7f\u7528golang\u8fdb\u884c\u91cd\u5199\u4e0b\uff0c\u8fd9\u6837\u5373\u4f7f\u8ba9\u5176\u4ed6\u4eba\u8c03\u7528\u7684\u8bdd\uff0c\u4e5f\u4e0d\u4f1a\u6cc4\u9732\u7f51\u5173\u7684\u4fe1\u606f\u548c\u8c03\u7528\u65b9\u5f0f \uff0c\u521a\u597d\u4e5f\u501f\u6b64\u673a\u4f1a\u603b\u7ed3\u4e0bgolang\u4e0bpost\u548cget\u6570\u636e\u7684\u65b9\u6cd5\u3002<\/p>\n

\u4e00\u3001http get\u8bf7\u6c42<\/strong><\/div>\n

\u7531\u4e8eget\u8bf7\u6c42\u76f8\u5bf9\u7b80\u5355\uff0c\u8fd9\u91cc\u5148\u770b\u4e0b\u5982\u679c\u901a\u8fc7\u4e00\u4e2aURL get\u6570\u636e\uff1a<\/p>\n

\/*\r\nHttp (curl) request in golang\r\n@author www.361way.com <itybku@139.com>\r\n*\/\r\npackage main\r\nimport (\r\n\"fmt\"\r\n\"io\/ioutil\"\r\n\"net\/http\"\r\n)\r\nfunc main() {\r\nurl := \"https:\/\/361way.com\/api\/users\"\r\nreq, _ := http.NewRequest(\"GET\", url, nil)\r\nres, _ := http.DefaultClient.Do(req)\r\ndefer res.Body.Close()\r\nbody, _ := ioutil.ReadAll(res.Body)\r\nfmt.Println(string(body))\r\n}\r\n<\/pre>\n

\u5982\u9700\u8981\u589e\u52a0http header\u5934\uff0c\u53ea\u9700\u8981\u5728req\u4e0b\u589e\u52a0\u5bf9\u5e94\u7684\u5934\u4fe1\u606f\u5373\u53ef\uff0c\u5982\u4e0b\uff1a<\/p>\n

req, _ := http.NewRequest(\"GET\", url, nil)\r\nreq.Header.Add(\"cache-control\", \"no-cache\")\r\nres, _ := http.DefaultClient.Do(req)\r\n<\/pre>\n
\u4e8c\u3001http post\u8bf7\u6c42<\/strong><\/div>\n

http post\u8bf7\u6c42\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\/*\r\nHttp (curl) request in golang\r\n@author www.361way.com <itybku@139.com>\r\n*\/\r\npackage main\r\nimport (\r\n\"fmt\"\r\n\"net\/http\"\r\n\"io\/ioutil\"\r\n)\r\nfunc main() {\r\nurl := \"https:\/\/reqres.in\/api\/users\"\r\npayload := strings.NewReader(\"name=test&jab=teacher\")\r\nreq, _ := http.NewRequest(\"POST\", url, payload)\r\nreq.Header.Add(\"content-type\", \"application\/x-www-form-urlencoded\")\r\nreq.Header.Add(\"cache-control\", \"no-cache\")\r\nres, _ := http.DefaultClient.Do(req)\r\ndefer res.Body.Close()\r\nbody, _ := ioutil.ReadAll(res.Body)\r\nfmt.Println(string(body))\r\n}\r\n<\/pre>\n

\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684\u662f\uff0c\u4e0a\u9762\u7684req.Header.Add\u00a0 x-www-form-urlencoded \u884c\u662f\u5173\u952e\uff0c\u4e00\u5b9a\u4e0d\u80fd\u53d6\u6d88\uff0c\u6211\u5728post\u7ed9\u7f51\u5173\u6570\u636e\u65f6\uff0c\u521a\u5f00\u59cb\u4e00\u76f4\u4e0d\u6210\u529f\uff08\u4e5f\u672a\u62a5\u9519\uff09\uff0c\u540e\u6765\u52a0\u4e0a\u8fd9\u4e00\u884c\u540e\u5c31\u6210\u529f\u53d1\u9001\u4fe1\u606f\u4e86\u3002<\/p>\n

\u4e09\u3001post\u00a0bytes<\/strong><\/div>\n

\u4e0a\u9762\u76f4\u63a5post\u7684\u662f\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4ee5post bytes\u6570\u636e\u7ed9URL ,\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n

package main\r\nimport (\r\n\"bytes\"\r\n\"fmt\"\r\n\"io\/ioutil\"\r\n\"net\/http\"\r\n\"net\/url\"\r\n)\r\nfunc main() {\r\nrequest_url := \"http:\/\/localhost\/index.php\"\r\n\/\/ \u8981 POST\u7684 \u53c2\u6570\r\nform := url.Values{\r\n\"username\": {\"xiaoming\"},\r\n\"address\": {\"beijing\"},\r\n\"subject\": {\"Hello\"},\r\n\"from\": {\"china\"},\r\n}\r\n\/\/ func Post(url string, bodyType string, body io.Reader) (resp *Response, err error) {\r\n\/\/\u5bf9form\u8fdb\u884c\u7f16\u7801\r\nbody := bytes.NewBufferString(form.Encode())\r\nrsp, err := http.Post(request_url, \"application\/x-www-form-urlencoded\", body)\r\nif err != nil {\r\npanic(err)\r\n}\r\ndefer rsp.Body.Close()\r\nbody_byte, err := ioutil.ReadAll(rsp.Body)\r\nif err != nil {\r\npanic(err)\r\n}\r\nfmt.Println(string(body_byte))\r\n}\r\n\u540c\u6837\uff0c\u6d89\u53ca\u5230http\u5934\u7684\u65f6\u5019\uff0c\u548c\u4e0a\u9762\u4e00\u6837\uff0c\u901a\u8fc7\u4e0b\u9762\u7684\u65b9\u5f0f\u589e\u52a0\uff1a\r\n\r\nreq, err := http.NewRequest(\"POST\", hostURL, strings.NewReader(publicKey))\r\nif err != nil {\r\nglog.Fatal(err)\r\n}\r\nreq.Header.Add(\"Content-Type\", \"application\/x-www-form-urlencoded\")\r\n<\/pre>\n

\u6700\u540e\uff0c\u63a8\u8350\u591a\u770b\u5b98\u65b9\u6587\u6863\uff0c\u521a\u5f00\u59cb\u627e\u4e86\u4e0d\u5c11\u6587\u6863\u4e00\u76f4\u4e0d\u5f97\u8981\u9886\uff0c\u800c\u5b98\u65b9request_test.go \u6587\u4ef6<\/a>\u5df2\u7ed9\u4e86\u6211\u4eec\u5f88\u597d\u7684\u793a\u4f8b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u4e4b\u524d\u4f7f\u7528python\u8fdb\u884c\u7f16\u7a0b\u7684\u65f6\u5019\uff0c\u6700\u5e38\u7528\u7684\u5c31\u662f\u901a\u8fc7post\u548cget\u4e00\u4e2aURL\u6293\u53d6\u6240\u9700\u7684\u6570\u636e\uff0c\u4e4b\u524d\u6709\u4e00\u4e2a\u77ed\u4fe1\u63a5 […]<\/p>\n","protected":false},"author":643,"featured_media":128928,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-128926","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/128926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=128926"}],"version-history":[{"count":2,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/128926\/revisions"}],"predecessor-version":[{"id":128987,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/128926\/revisions\/128987"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/128928"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=128926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=128926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=128926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}