乐趣区

关于http:HTTP中post方法提交不同格式的数据

http 提交数据次要通过 post 办法实现,在提交不同格局的数据时最大的不同点在于数据的组织模式不同,同时须要设置不同格局对应的 Content-type 格局

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": "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() {
   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-60e47872-48e5cb7c65ce959f03544ffb"
   //},
   // "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.Close()
   // 以上数据组织实现
   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": "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(){//postForm() //post 表单
   //postJson() //post Json
   postFile() //post file}
退出移动版