{"id":918,"date":"2022-02-27T11:22:37","date_gmt":"2022-02-27T03:22:37","guid":{"rendered":"https:\/\/www.luja.cc\/?p=918"},"modified":"2022-02-27T11:27:14","modified_gmt":"2022-02-27T03:27:14","slug":"go%e6%89%a7%e8%a1%8cmultipart-form-data%e7%b1%bb%e5%9e%8bpost%e6%97%b6%e6%97%a0%e6%b3%95%e6%88%90%e5%8a%9f%e9%97%ae%e9%a2%98%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.luja.cc\/?p=918","title":{"rendered":"go\u6267\u884cmultipart\/form-data\u7c7b\u578bPOST\u65f6\u65e0\u6cd5\u6210\u529f\u95ee\u9898\u5904\u7406"},"content":{"rendered":"\n<p>\u6700\u8fd1\u5728\u7528go\u8bed\u8a00\u505a\u5de8\u91cf\u89c6\u9891\u4e0a\u4f20\u63a5\u53e3\u65f6\u9047\u5230\u95ee\u9898\uff0c\u8bb0\u5f55\u4e00\u4e0b\u89e3\u51b3\u8fc7\u7a0b<\/p>\n\n\n\n<p>\u5de8\u91cf\u89c6\u9891\u4e0a\u4f20\u63a5\u53e3\u663e\u793a\u662f\u8981\u7528multipart\/form-data\u7c7b\u578b\u6570\u636e\u6765\u8fdb\u884cPOST<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227100320.jpg\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"516\" src=\"https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227100320-1024x516.jpg\" alt=\"\" class=\"wp-image-920\" srcset=\"https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227100320-1024x516.jpg 1024w, https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227100320-300x151.jpg 300w, https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227100320-768x387.jpg 768w, https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227100320-1536x773.jpg 1536w, https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227100320.jpg 1726w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>\u7136\u540e\u6211\u5148\u7528go\u81ea\u5e26\u7684multipart\u5e93\u8bf7\u6c42\u4e86\u4e00\u4e0b\uff0c\u5374\u8fd4\u56de\u4e86\u9519\u8bef\uff1aNo permission to operate account 0\uff0c\u610f\u601d\u662f\u6ca1\u6709\u6743\u9650\u64cd\u4f5c\u8d26\u62370\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tbodyBuf := &amp;bytes.Buffer{}\r\n\tbodyWriter := multipart.NewWriter(bodyBuf)\r\n\r\n\t_ = bodyWriter.WriteField(\"advertiser_id\", \"1724433764965395\")\r\n\t_ = bodyWriter.WriteField(\"video_signature\", md5str)\r\n\tfile, _ := os.Open(\".\/v.mp4\")\r\n\tdefer file.Close()\r\n\tpart, _ := bodyWriter.CreateFormFile(\"video_file\", \"v.mp4\")\r\n\t_, _ = io.Copy(part, file)\r\n\r\n\turl := \"https:\/\/ad.oceanengine.com\/open_api\/2\/file\/video\/ad\/\"\r\n\tclientOcean := &amp;http.Client{}\r\n\treq, _ := http.NewRequest(\"POST\", url, bodyBuf)\r\n\treq.Header.Set(\"Access-Token\", \"cf3eac6784537bd4def62a1f6548cf3f679349a3\")\r\n\treq.Header.Set(\"X-Debug-Mode\", \"1\")\r\n\treq.Header.Set(\"Content-Type\", bodyWriter.FormDataContentType())\r\n\tdefer bodyWriter.Close()\r\n\r\r\n\tresp, err := clientOcean.Do(req)\r\n\tcheck(err)\r\n\tdefer resp.Body.Close()\r\n\tres, _ := ioutil.ReadAll(resp.Body)\r\n\tfmt.Println(string(res))\r\n\tpanic(\"ok\")<\/code><\/pre>\n\n\n\n<p>\u8fd4\u56de\u9519\u8bef\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227104520.jpg\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"193\" src=\"https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227104520-1024x193.jpg\" alt=\"\" class=\"wp-image-921\" srcset=\"https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227104520-1024x193.jpg 1024w, https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227104520-300x57.jpg 300w, https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227104520-768x145.jpg 768w, https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227104520-1536x290.jpg 1536w, https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227104520.jpg 1930w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>\u9519\u8bef\u53ef\u4ee5\u770b\u51fa\uff0crequest_params\u4e3a\u7a7a\uff0c\u5f88\u660e\u663e\u5de8\u91cf\u90a3\u8fb9\u6ca1\u6709\u83b7\u53d6\u5230\u4efb\u4f55\u6570\u636e\uff0c\u540e\u6765\u5728\u7f51\u4e0a\u67e5\u4e86\u5f88\u591a\u8d44\u6599\uff0c\u90fd\u6ca1\u627e\u5230\u89e3\u51b3\u529e\u6cd5\uff0c\u8fd9\u4e2a\u95ee\u9898\u5df2\u7ecf\u56f0\u6270\u4e86\u6211\u4e24\u5929\u3002<\/p>\n\n\n\n<p>\u540e\u6765\u6211\u627e\u6765\u4e86\u51e0\u4e2a\u4e09\u65b9\u8bf7\u6c42\u5e93\uff0c\u95ee\u9898\u4f9d\u65e7\uff0c\u76f4\u5230\u6211\u7528\u4e86httpclient\u5e93\uff08github.com\/ddliu\/go-httpclient\uff09\uff0c\u53d1\u73b0\u7adf\u7136\u80fd\u6b63\u5e38\u8bf7\u6c42\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tresp2, err := httpclient.\r\n\t\tWithHeader(\"Access-Token\", \"cf3eac6784537bd4def62a1f6548cf3f679349a3\").\r\n\t\tWithHeader(\"X-Debug-Mode\", \"1\").\r\n\t\tPost(url, map&#91;string]string {\r\n\t\t\t\"advertiser_id\": \"1724433764965395\",\r\n\t\t\t\"video_signature\": md5str,\r\n\t\t\t\"@video_file\": \".\/v.mp4\",\r\n\t\t})\r\n\tcheck(err)\r\n\tbody1, _ := resp2.ToString()\r\n\tfmt.Println(body1)<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u6211\u7528httputil\u6765\u8f93\u51fa\u8bf7\u6c42\u5143\u6570\u636e\uff0c\u610f\u5916\u53d1\u73b0\u4e24\u8005\u5c45\u7136\u6709\u7ec6\u5fae\u7684\u5dee\u522b\uff0c\u6ca1\u7528\u4e09\u65b9\u7c7b\u5e93\u7684\u8bf7\u6c42\u7ed3\u5c3e\u7adf\u7136\u5c11\u4e86\u4e00\u4e2a\u7ed3\u675fboundary\u754c\u5b9a\u7b26\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227110331.jpg\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"481\" src=\"https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227110331-1024x481.jpg\" alt=\"\" class=\"wp-image-922\" srcset=\"https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227110331-1024x481.jpg 1024w, https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227110331-300x141.jpg 300w, https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227110331-768x360.jpg 768w, https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227110331-1536x721.jpg 1536w, https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227110331.jpg 1626w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227110527.jpg\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"453\" src=\"https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227110527-1024x453.jpg\" alt=\"\" class=\"wp-image-923\" srcset=\"https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227110527-1024x453.jpg 1024w, https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227110527-300x133.jpg 300w, https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227110527-768x340.jpg 768w, https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227110527-1536x680.jpg 1536w, https:\/\/www.luja.cc\/wp-content\/uploads\/2022\/02\/Dingtalk_20220227110527.jpg 1873w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption>\u53ef\u4ee5\u5f88\u660e\u663e\u770b\u5230\u7528httpclient\u5e93\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u591a\u4e86\u4e00\u4e2a&#8211;4b9b558b03b4b6d0caa0ef392615603f368bbfee0c0d2ead26e7dd61cca5&#8211;\uff0c\u8fd9\u4e2a\u7ed3\u675f\u7b26\u8d77\u4e86\u5173\u952e\u4f5c\u7528\uff0c\u81f3\u6b64\u5df2\u7ecf\u627e\u5230\u4e86\u95ee\u9898\u6240\u5728\uff0c\u53ea\u8981\u89e3\u51b3\u8fd9\u4e2a\u5c31\u53ef\u4ee5\u4e86\u3002<\/figcaption><\/figure>\n\n\n\n<p>\u540e\u6765\u7ecf\u8fc7\u6392\u67e5\uff0c\u53d1\u73b0\u7adf\u7136\u548cdefer bodyWriter.Close()\u8fd9\u884c\u6709\u5173\uff0c\u731c\u6d4b\u6709\u53ef\u80fd\u662fbodyWriter\u8fc7\u65e9\u5173\u95ed\u5bfc\u81f4\uff0c\u7528\u53d8\u91cf\u83b7\u53d6FormDataContentType\uff0c\u518d\u6539\u6210err1 := bodyWriter.Close()\uff0c\u79fb\u5230\u524d\u9762\u5c31\u53ef\u4ee5\u4e86\uff0c\u6700\u7ec8\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\r\n\r\nimport (\r\n\t\"os\"\r\n\t\"io\"\r\n\t\"fmt\"\r\n\t\"bytes\"\r\n\t\"net\/http\"\r\n\t\"io\/ioutil\"\r\n\t\"crypto\/md5\"\r\n\t\"mime\/multipart\"\r\n\t\"net\/http\/httputil\"\r\n\t\"github.com\/ddliu\/go-httpclient\"\r\n)\r\n\r\nfunc main() {\r\n\tf, _ := os.Open(\".\/v.mp4\")\r\n\tdefer f.Close()\r\n\tmd5Handle := md5.New()\r\n\t_, _ = io.Copy(md5Handle, f)\r\n\tmd := md5Handle.Sum(nil)\r\n\tmd5str := fmt.Sprintf(\"%x\", md)\r\n\r\n\tbodyBuf := &amp;bytes.Buffer{}\r\n\tbodyWriter := multipart.NewWriter(bodyBuf)\r\n\r\n\t_ = bodyWriter.WriteField(\"advertiser_id\", \"1724433764965395\")\r\n\t_ = bodyWriter.WriteField(\"video_signature\", md5str)\r\n\tfile, _ := os.Open(\".\/v.mp4\")\r\n\tdefer file.Close()\r\n\tpart, _ := bodyWriter.CreateFormFile(\"video_file\", \"v.mp4\")\r\n\t_, _ = io.Copy(part, file)\r\n\r\n\turl := \"https:\/\/ad.oceanengine.com\/open_api\/2\/file\/video\/ad\/\"\r\n\tcontentType := bodyWriter.FormDataContentType()\r\n\terr1 := bodyWriter.Close()\r\n\tcheck(err1)\r\n\r\n\tclientOcean := &amp;http.Client{}\r\n\treq, _ := http.NewRequest(\"POST\", url, bodyBuf)\r\n\treq.Header.Set(\"Access-Token\", \"cf3eac6784537bd4def62a1f6548cf3f679349a3\")\r\n\treq.Header.Set(\"X-Debug-Mode\", \"1\")\r\n\treq.Header.Set(\"Content-Type\", contentType)\r\n\r\n\tdump, err := httputil.DumpRequestOut(req, true)\r\n\tif err == nil {\r\n\t\tfmt.Printf(\"%s\\n\", dump)\r\n\t}\r\n\tresp, err := clientOcean.Do(req)\r\n\tcheck(err)\r\n\tdefer resp.Body.Close()\r\n\tres, _ := ioutil.ReadAll(resp.Body)\r\n\tfmt.Println(string(res))\r\n\tpanic(\"ok\")\r\n\r\n\t\/\/ post file(multipart) use httpclient\r\n\tresp2, err := httpclient.\r\n\t\tWithHeader(\"Access-Token\", \"cf3eac6784537bd4def62a1f6548cf3f679349a3\").\r\n\t\tWithHeader(\"X-Debug-Mode\", \"1\").\r\n\t\tWithOption(httpclient.OPT_DEBUG, true).\r\n\t\tPost(url, map&#91;string]string {\r\n\t\t\t\"advertiser_id\": \"1724433764965395\",\r\n\t\t\t\"video_signature\": md5str,\r\n\t\t\t\"@video_file\": \".\/v.mp4\",\r\n\t\t})\r\n\tcheck(err)\r\n\tbody1, _ := resp2.ToString()\r\n\tfmt.Println(body1)\r\n}\r\n\r\nfunc check(err error) {\r\n\tif err != nil{\r\n\t\tfmt.Println(err)\r\n\t\tpanic(err)\r\n\t}\r\n}<\/code><\/pre>\n\n\n\n<p>\u81f3\u6b64\uff0c\u95ee\u9898\u5f97\u5230\u5b8c\u7f8e\u89e3\u51b3\uff0c\u8fd9\u5bf9\u4e8e\u4e0d\u559c\u6b22\u7528\u4e09\u65b9\u7c7b\u5e93\u7684\u662f\u4e2a\u597d\u65b9\u6cd5\uff0c\u6bd5\u7adf\u7528\u4e09\u65b9\u7c7b\u5e93\u7f16\u8bd1\u51fa\u6765\u7684\u6587\u4ef6\u8981\u7a0d\u5fae\u5927\u4e00\u70b9\uff0c\u5206\u4eab\u4e00\u4e0b\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u5427\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u7528go\u8bed\u8a00\u505a\u5de8\u91cf\u89c6\u9891\u4e0a\u4f20\u63a5\u53e3\u65f6\u9047\u5230\u95ee\u9898\uff0c\u8bb0\u5f55\u4e00\u4e0b\u89e3\u51b3\u8fc7\u7a0b \u5de8\u91cf\u89c6\u9891\u4e0a\u4f20\u63a5\u53e3\u663e\u793a\u662f\u8981\u7528multipart\/ &hellip;<br \/><a href=\"https:\/\/www.luja.cc\/?p=918\">Read more <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[186],"tags":[],"_links":{"self":[{"href":"https:\/\/www.luja.cc\/index.php?rest_route=\/wp\/v2\/posts\/918"}],"collection":[{"href":"https:\/\/www.luja.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.luja.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.luja.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.luja.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=918"}],"version-history":[{"count":1,"href":"https:\/\/www.luja.cc\/index.php?rest_route=\/wp\/v2\/posts\/918\/revisions"}],"predecessor-version":[{"id":924,"href":"https:\/\/www.luja.cc\/index.php?rest_route=\/wp\/v2\/posts\/918\/revisions\/924"}],"wp:attachment":[{"href":"https:\/\/www.luja.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.luja.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.luja.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}