{"id":207584,"date":"2025-07-08T13:36:01","date_gmt":"2025-07-08T05:36:01","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207584\/"},"modified":"2025-07-08T13:36:01","modified_gmt":"2025-07-08T05:36:01","slug":"%e4%bd%bf%e7%94%a8curl%e8%bf%9b%e8%a1%8c%e6%b5%8b%e8%af%95%e6%98%af%e6%9c%89%e6%95%88%e7%9a%84%ef%bc%8c%e4%bd%86%e6%98%af%e7%9b%b8%e5%90%8c%e7%9a%84post%e4%bc%9a%e5%a4%b1%e8%b4%a5%ef%bc%8c%e5%9b%a0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207584\/","title":{"rendered":"\u4f7f\u7528curl\u8fdb\u884c\u6d4b\u8bd5\u662f\u6709\u6548\u7684\uff0c\u4f46\u662f\u76f8\u540c\u7684POST\u4f1a\u5931\u8d25\uff0c\u56e0\u4e3a\u201chttptest.NewRequest\u201d"},"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\u7528curl\u8fdb\u884c\u6d4b\u8bd5\u662f\u6709\u6548\u7684\uff0c\u4f46\u662f\u76f8\u540c\u7684POST\u4f1a\u5931\u8d25\uff0c\u56e0\u4e3a\u201chttptest.NewRequest\u201d<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 19:24: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>\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\u4f7f\u7528curl\u8fdb\u884c\u6d4b\u8bd5\u662f\u6709\u6548\u7684\uff0c\u4f46\u662f\u76f8\u540c\u7684POST\u4f1a\u5931\u8d25\uff0c\u56e0\u4e3a\u201chttptest.NewRequest\u201d\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> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u4f7f\u7528\u8fd9\u4e2acurl\u547d\u4ee4\uff0c\u6211\u53ef\u4ee5\u5728\u540e\u7aef\u521b\u5efa\u90e8\u4ef6\u3002\u8bf7\u6c42\u5df2\u6210\u529f\u9a8c\u8bc1\u3002<\/p>\n<p><code>curl -xpost -h\"content-type: application\/json\" localhost:8080\/v1\/parts\/ -d'{\"custom_id\":\"test\"}' -d -<\/code><\/p>\n<p>\u4f46\u662f\uff0c\u5982\u679c\u6211\u53e6\u4e00\u65b9\u9762\u5c1d\u8bd5\u5728\u6d4b\u8bd5\u4e2d\u91cd\u65b0\u521b\u5efa\u8be5\u8bf7\u6c42\uff0c\u90a3\u4e48\u5b83\u4e0d\u4f1a\u88ab\u89c6\u4e3a\u6709\u6548\u7684\u8bf7\u6c42\uff1a<\/p>\n<p><strong>\u9519\u8bef\u6d88\u606f\uff1a<\/strong> \u201c\u65e0\u6cd5\u5c06\u8bf7\u6c42\u7ed1\u5b9a\u5230\u90e8\u4ef6\u201d<\/p>\n<p>\u6211\u4e0d\u660e\u767d\u4e3a\u4ec0\u4e48\u5f53\u5377\u66f2\u5de5\u4f5c\u65f6\u6d4b\u8bd5\u4f1a\u5931\u8d25\u3002\u8c01\u80fd\u53d1\u73b0\u9519\u8bef\u5417\uff1f<\/p>\n<p><strong>handlers\/parts_test.go<\/strong><\/p>\n<pre>func testcreatepart(t *testing.t) {\n        \/\/ setup\n        e := echo.new()\n\n        reqbody := strings.newreader(`{\"custom_id\": \"custom\"}`)\n\n        req := httptest.newrequest(http.methodpost, \"\/parts\", reqbody)\n        rec := httptest.newrecorder()\n        c := e.newcontext(req, rec)\n\n        if assert.noerror(t, createpart(c)) {\n                assert.equal(t, http.statusok, rec.code)\n                assert.equal(t, \"userjson\", rec.body.string())\n        }\n}<\/pre>\n<p><strong>handlers\/part.go<\/strong><\/p>\n<pre>func createpart(c echo.context) error {\n      resp := renderings.partresponse{}\n      pr := new(bindings.createpartrequest)\n\n      if err := c.bind(pr); err != nil {\n              resp.success = false\n              resp.message = \"unable to bind request to parts\"\n              return c.json(http.statusbadrequest, resp)\n      }\n\n      if err := pr.validate(c); err != nil {\n              resp.success = false\n              resp.message = err.error()\n              return c.json(http.statusbadrequest, resp)\n      }<\/pre>\n<p><strong>\u5185\u90e8\/\u7ed1\u5b9a\/parts.go<\/strong><\/p>\n<pre>package bindings\n\n  import (\n          \"fmt\"\n\n          \"github.com\/labstack\/echo\"\n  )\n\ntype CreatePartRequest struct {\n          CustomID string `json:\"custom_id\" xml:\"custom_id\" form:\"custom_id\" query:\"custom_id\"`\n  }\n\nfunc (pr CreatePartRequest) Validate(c echo.Context) error {\n          errs := new(RequestErrors)\n          fmt.Println(\"pr: \", pr.CustomID)\n          if pr.CustomID == \"\" {\n                  errs.Append(ErrCustomIDEmpty)\n          }\n          if errs.Len() == 0 {\n                  return nil\n          }\n          return errs\n  }<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5c1d\u8bd5\u6dfb\u52a0 <code>req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)<\/code> \u5b83\u53ef\u80fd\u4e0e\u5185\u5bb9\u7c7b\u578b\u6807\u5934\u6709\u5173\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u4f7f\u7528curl\u8fdb\u884c\u6d4b\u8bd5\u662f\u6709\u6548\u7684\uff0c\u4f46\u662f\u76f8\u540c\u7684POST\u4f1a\u5931\u8d25\uff0c\u56e0\u4e3a\u201chttptest.NewRequest\u201d\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\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-207584","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207584","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=207584"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207584\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}