HTTP submission of data is mainly achieved by POST method. The biggest difference in submission of data of different formats lies in the different organizational forms of data, and the Content-Type format corresponding to different formats should be set at the same time

package main import ( "bytes" "encoding/json" "fmt" "io" "io/ioutil" "mime/multipart" "net/http" "net/url" "os" "strings" ) func postForm(){ data := make(url.Values) data.Add("name", "yuan") data.Add("age", "18") payload := data.Encode() resp, err := http.Post("http://httpbin.org/post", "application/x-www-form-urlencoded", strings.NewReader(payload)) if err ! = nil { panic(err) } defer func() {_ = resp.Body.Close()}() content, err := ioutil.ReadAll(resp.Body) fmt.Printf("%s", content) //{ // "args": {}, // "data": "", // "files": {}, // "form": { // "age": "18", // "name": "yuan" // }, // "headers": { // "Accept-Encoding": "gzip", // "Content-Length": "16", // "Content-Type": "application/x-www-form-urlencoded", // "Host": "httpbin.org", // "User-Agent": // "X-amzn-trace-id ": "Root=1-60e4770f-2a5448187a3ae2d560ebf3ff" //}, // "json": Null, // "origin": "222.211.214.252", // "url": "http://httpbin.org/post" //} } func postJson() { u := struct { Name string `json:"name"` Age int `json:"age"` }{ Name: "yuan", Age: 18, } payload, _ := json.Marshal(u) //data := make(url.Values) //data.Add("name", "yuan") //data.Add("age", "18") //payload := data.Encode() resp, err := http.Post("http://httpbin.org/post", "application/json", bytes.NewReader(payload)) if err ! = nil { panic(err) } defer func() { _ = resp.Body.Close() }() content, err := ioutil.ReadAll(resp.Body) fmt.Printf("%s", content) //{ // "args": {}, // "data": "{\"name\":\"yuan\",\"age\":18}", // "files": {}, // "form": {}, // "headers": { // "Accept-Encoding": "gzip", // "Content-Length": "24", // "Content-Type": "application/json", // "Host": "httpbin.org", // "User-Agent": "Go - HTTP client / 1.1" / / "X - Amzn - Trace - Id" : "Root = 1-60 e47872-48 e5cb7c65ce959f03544ffb" / /}, / / "json" : {/ / "age" : 18, / / "name" : "yuan" / /}, / / "origin" : "222.211.214.252," / / "url" : "http://httpbin.org/post" //} } func postFile(){ body := &bytes.Buffer{} writer := multipart.NewWriter(body) upload1, _ := writer.CreateFormFile("formName1", "test.txt") uploadFile1, _:= os.Open("test.txt") defer uploadFile1.Close() io.Copy(upload1, uploadFile1) upload2, _ := writer.CreateFormFile("formName2", "test.txt") uploadFile2, _:= os.Open("test.txt") defer uploadFile2.Close() io.Copy(upload2, UploadFile2) writer. The Close (). / / the above data organization to complete the FMT Println (writer) FormDataContentType (), resp. err := http.Post("http://httpbin.org/post", writer.FormDataContentType(), body) if err ! = nil { panic(err) } defer func() {_ = resp.Body.Close()}() content, _ := ioutil.ReadAll(resp.Body) fmt.Printf("%s", content) //{ // "args": {}, // "data": "", // "files": { // "formName1": "qwqwertwertwertwert", // "formName2": "qwqwertwertwertwert" // }, // "form": {}, // "headers": { // "Accept-Encoding": "gzip", // "Content-Length": "462", // "Content-Type": "multipart/form-data; boundary=c7d43bcbc370e8f128df077361e7f5551970ee279eb483fc4c1a7016e68f", // "Host": "httpbin.org", // "User-Agent": // "X-amzn-trace-id ": "Root=1-60e4806a-501ee1f8562afc5c74143bcf" //}, // "json": Null, // "origin": "222.211.214.252", // "url": "http://httpbin.org/post" //}} func main(){// PostForm () // PostJSON () // PostJSON () // Post File() // Post File}