{"id":207375,"date":"2025-07-08T12:05:40","date_gmt":"2025-07-08T04:05:40","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207375\/"},"modified":"2025-07-08T12:05:40","modified_gmt":"2025-07-08T04:05:40","slug":"%e4%bd%bf%e7%94%a8http-newrequest%e8%b0%83%e7%94%a8rest-api%e6%97%b6%ef%bc%8c%e5%93%8d%e5%ba%94%e6%ad%a3%e6%96%87%e4%b9%b1%e7%a0%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207375\/","title":{"rendered":"\u4f7f\u7528http.NewRequest\u8c03\u7528rest API\u65f6\uff0c\u54cd\u5e94\u6b63\u6587\u4e71\u7801"},"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>\u4f7f\u7528http.NewRequest\u8c03\u7528rest API\u65f6\uff0c\u54cd\u5e94\u6b63\u6587\u4e71\u7801<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 23:42:39<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9Golang\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300a\u4f7f\u7528http.NewRequest\u8c03\u7528rest API\u65f6\uff0c\u54cd\u5e94\u6b63\u6587\u4e71\u7801\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u5c1d\u8bd5\u4f7f\u7528 go \u8c03\u7528 api\u3002\u4f7f\u7528 postman \u65f6\u4e00\u5207\u6b63\u5e38\u3002\u4f46\u5982\u679c\u6211\u4f7f\u7528 postman \u7684 go \u4ee3\u7801\uff0c\u54cd\u5e94\u5c31\u4f1a\u51fa\u73b0\u4e71\u7801\/\u4e0d\u6e05\u695a\u3002<\/p>\n<p>\u5728\u6211\u6b63\u5728\u4f7f\u7528\u7684\u4ee3\u7801\u4e0b\u65b9\uff1a<\/p>\n<pre>func callapi() {\n    url := \"https:\/\/url\"\n\n    req, _ := http.newrequest(\"get\", url, nil)\n\n    req.header.add(\"authorization\", \"bearer token is normaly here\")\n    req.header.add(\"user-agent\", \"postmanruntime\/7.19.0\")\n    req.header.add(\"accept\", \"accept: application\/json\")\n    req.header.add(\"cache-control\", \"no-cache\")\n    req.header.add(\"postman-token\", \"postman token normaly here\")\n    req.header.add(\"host\", \"host normaly here\")\n    req.header.add(\"accept-encoding\", \"gzip, deflate\")\n    req.header.add(\"connection\", \"keep-alive\")\n    req.header.add(\"cache-control\", \"no-cache\")\n\n    res, _ := http.defaultclient.do(req)\n\n    defer res.body.close()\n    body, _ := ioutil.readall(res.body)\n\n\n    fmt.println(string(body))\n}<\/pre>\n<p>\u5f53\u6211\u4f7f\u7528 <code>fmt.println(string(body))<\/code> \u65f6\u5f97\u5230\u7684\u54cd\u5e94\u5982\u4e0b\u6240\u793a\u3002\u6211\u8fd8\u4f7f\u7528\u6b64\u4ee3\u7801\u5c1d\u8bd5\u4e86\u5176\u4ed6 api\uff0c\u5e76\u5f97\u5230\u4e86\u76f8\u540c\u7684\u7ed3\u679c\u3002 <\/p>\n<pre>r\ufffd\u75f1\ufffd<\/pre>\n<p>\u6211\u8fd8\u5c1d\u8bd5\u5c06 json \u89e3\u7ec4\u4e3a\u7ed3\u6784\uff0c\u4f46\u51fa\u73b0\u4ee5\u4e0b\u9519\u8bef <code>\u5bfb\u627e\u503c\u5f00\u5934\u7684\u65e0\u6548\u5b57\u7b26\u201c\\x1f\u201d<\/code><\/p>\n<p>\u6211\u8ba4\u4e3a\u8fd9\u4e0e\u89e3\u7801\u6709\u5173\u3002\u4f46\u6211\u4e0d\u77e5\u9053\u662f\u4ec0\u4e48\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u8981\u6c42\u670d\u52a1\u5668\u53d1\u9001\u538b\u7f29\u7684\u5185\u5bb9\uff08<code>req.header.add(\"accept-encoding\", \"gzip, deflate\")<\/code>\uff09\uff0c\u8fd9\u5c31\u662f\u60a8\u5f97\u5230\u7684\uff1agzip \u54cd\u5e94\uff0c\u7531\u54cd\u5e94\u6807\u5934\u6307\u793a\uff1a <code>\u5185\u5bb9\u7f16\u7801\uff1a[gzip]<\/code>\u3002<\/p>\n<p>\u5220\u9664\u8be5\u6807\u5934\uff08\u4e0d\u8981\u8bbe\u7f6e <code>accept-encoding<\/code> \u8bf7\u6c42\u6807\u5934\uff09\uff0c\u60a8\u5e94\u8be5\u83b7\u5f97\u7eaf json \u54cd\u5e94\u3002\u6216\u8005\u81ea\u5df1\u89e3\u7801 gzip \u54cd\u5e94\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u7701\u7565\u6b64\u6807\u5934\uff0c\u9ed8\u8ba4\u4f20\u8f93\u4ecd\u4f1a\u8bf7\u6c42 <code>gzip<\/code> \u7f16\u7801\uff0c\u4f46\u4e5f\u4f1a\u900f\u660e\u5730\u5bf9\u5176\u8fdb\u884c\u89e3\u7801\u3002\u7531\u4e8e\u60a8\u660e\u786e\u8bf7\u6c42\u5b83\uff0c\u56e0\u6b64\u4e0d\u4f1a\u53d1\u751f\u900f\u660e\u7684\u81ea\u52a8\u89e3\u7801\u3002\u8fd9\u8bb0\u5f55\u5728 \u5b57\u6bb5\u4e2d\uff1a<\/p>\n<pre>\/\/ DisableCompression, if true, prevents the Transport from\n\/\/ requesting compression with an \"Accept-Encoding: gzip\"\n\/\/ request header when the Request contains no existing\n\/\/ Accept-Encoding value. If the Transport requests gzip on\n\/\/ its own and gets a gzipped response, it's transparently\n\/\/ decoded in the Response.Body. However, if the user\n\/\/ explicitly requested gzip it is not automatically\n\/\/ uncompressed.\nDisableCompression bool<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u4f7f\u7528http.NewRequest\u8c03\u7528rest API\u65f6\uff0c\u54cd\u5e94\u6b63\u6587\u4e71\u7801\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\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; &#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-207375","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207375","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=207375"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207375\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}