HTTP data is submitted through post. The biggest difference between submitting data in different formats is that the data is organized in different ways and the content-Type format corresponding to different formats needs to be set

package main

import (
   "bytes"
   "encoding/json"
   "fmt"
   "io"
   "io/ioutil"
   "mime/multipart"
   "net/http"
   "net/url"
   "os"
   "strings"
)

func postForm(a){
   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))
   iferr ! =nil {
      panic(err)
   }

   defer func(a) {_ = 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",
   / / "the user-agent" : "Go - HTTP client / 1.1",
   // "X-Amzn-Trace-Id": "Root=1-60e4770f-2a5448187a3ae2d560ebf3ff"
   / /},
   // "json": null,
   / / "origin", "222.211.214.252",
   // "url": "http://httpbin.org/post"
   / /}
}

func postJson(a) {
   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))
   iferr ! =nil {
      panic(err)
   }

   defer func(a) { _ = 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",
   / / "the user-agent" : "Go - HTTP client / 1.1",
   // "X-Amzn-Trace-Id": "Root=1-60e47872-48e5cb7c65ce959f03544ffb"
   / /},
   // "json": {
   // "age": 18,
   // "name": "yuan"
   / /},
   / / "origin", "222.211.214.252",
   // "url": "http://httpbin.org/post"
   / /}
}

func postFile(a){
   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.Close()
   // The above data is organized
   fmt.Println(writer.FormDataContentType())
   resp, err := http.Post("http://httpbin.org/post", writer.FormDataContentType(), body)
   iferr ! =nil {
      panic(err)
   }
   defer func(a) {_ = 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",
   / / "the user-agent" : "Go - HTTP client / 1.1",
   // "X-Amzn-Trace-Id": "Root=1-60e4806a-501ee1f8562afc5c74143bcf"
   / /},
   // "json": null,
   / / "origin", "222.211.214.252",
   // "url": "http://httpbin.org/post"
   / /}
}

func main(a){
   / / postForm () / / post form
   //postJson() //post Json
   postFile() //post file
}
Copy the code