This is a creation in Article, where the information may have evolved or changed.
go post 上传文件
Package Mainimport ("bytes" "FMT" "io" "Mime/multipart" "net/http" "OS") func postfile (filenamestring, Target_urlstring) (*http. Response, error) {body_buf:= bytes. Newbufferstring ("") Body_writer:=multipart. Newwriter (BODY_BUF)//Use the body_writer-write the part headers to the buffer_, Err: = Body_writer. Createformfile ("UserFile", filename)ifErr! =Nil {fmt. Println ("error writing to buffer") returnnil, err}//The file data would be is the second part of the bodyFH, err: =OS. Open (filename)ifErr! =Nil {fmt. Println ("Error opening file") returnnil, err}//need to know the boundary to properly close the part myself.Boundary: =Body_writer. Boundary ()//close_string: = FMT. Sprintf ("\r\n--%s--\r\n", boundary)Close_buf: = bytes. Newbufferstring (FMT. Sprintf ("\r\n--%s--\r\n", boundary)) //Use Multi-reader to defer the reading of the file data until//writing to the socket buffer.Request_reader: =io. Multireader (Body_buf, FH, close_buf) fi, err:=FH. Stat ()ifErr! =Nil {fmt. Printf ("Error stating file:%s", filename)returnnil, err} req, err:= http. Newrequest ("POST", Target_url, Request_reader)ifErr! =Nil {returnnil, err}//Set headers for multipart, and Content LengthReq. Header.add ("Content-type","multipart/form-data; boundary="+boundary) Req. ContentLength= fi. Size () + Int64 (body_buf. Len ()) +Int64 (Close_buf. Len ())returnhttp. Defaultclient.do (req)}//Sample UsageFunc Main () {target_url:="Http://localhost:8086/upload"FileName:="/users/wei/downloads/21dian_1.9_10"postfile (filename, Target_url)} reference article https://MATT.AIMONETTI.NET/POSTS/2013/07/01/GOLANG-MULTIPART-FILE-UPLOAD-EXAMPLE/25Package Mainimport ("bytes" "FMT" "Log" "Mime/multipart" "net/http")//creates a new file upload HTTP request with optional extra paramsFunc newmultipartrequest (URLstring,paramsmap[string]string) (*http. Request, error) {body:= &bytes. buffer{} Writer:=multipart. Newwriter (body) forKey, Val: = Rangeparams { _ =writer. Writefield (Key, Val)} writer. Close ()returnhttp. Newrequest ("POST", URL, body)} Func Main () {extraparams:= map[string]string{ "title":"My Document", "author":"Zieckey", "Description":"A document with all the Go programming language Secrets",} request, err:= Newmultipartrequest ("Http://127.0.0.1:8091/echo", Extraparams)ifErr! =Nil {log. Fatal (ERR)} client:= &http. client{} resp, err:=client. Do (Request)ifErr! =Nil {log. Fatal (ERR)}Else{body:= &bytes. buffer{} _, Err:=body. Readfrom (resp. Body)ifErr! =Nil {log. Fatal (Err)} resp. Body.close () fmt. Println (resp. StatusCode) fmt. Println (resp. Header) fmt. Example of Println (body)}}multipart package Mainimport ("bytes" "FMT" "io" "Log" "Mime/multipart" "net/http" "OS" "Path/filepath")//creates a new file upload HTTP request with optional extra paramsFunc newfileuploadrequest (URIstring,paramsmap[string]string, ParamName, Pathstring) (*http. Request, error) {file, err:=OS. Open (PATH)ifErr! =Nil {returnNil, err} defer file. Close () Body:= &bytes. buffer{} Writer:=multipart. Newwriter (body) part, err:=writer. Createformfile (ParamName, filepath. Base (path))ifErr! =Nil {returnNil, err} _, Err=io. Copy (part, file) forKey, Val: = Rangeparams { _ =writer. Writefield (Key, Val)} Err=writer. Close ()ifErr! =Nil {returnnil, err} request, err:= http. Newrequest ("POST", URI, body) request. Header.add ("Content-type", writer. Formdatacontenttype ())returnrequest, Err}func Main () {path, _:=OS. GETWD () path+="/test.pdf"Extraparams:= map[string]string{ "title":"My Document", "author":"Matt Aimonetti", "Description":"A document with all the Go programming language Secrets",} request, err:= Newfileuploadrequest ("Http://localhost:8086/upload", Extraparams,"UserFile","/users/wei/downloads/21dian_1.9_10") ifErr! =Nil {log. Fatal (ERR)} client:= &http. client{} resp, err:=client. Do (Request)ifErr! =Nil {log. Fatal (ERR)}Else{body:= &bytes. buffer{} _, Err:=body. Readfrom (resp. Body)ifErr! =Nil {log. Fatal (Err)} resp. Body.close () fmt. Println (resp. StatusCode) fmt. Println (resp. Header) fmt. Println (Body)}}
Read the original