HTTP中post方法提交不同格式的数据
生活随笔
收集整理的這篇文章主要介紹了
HTTP中post方法提交不同格式的数据
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
http提交數(shù)據(jù)主要通過(guò)post方法實(shí)現(xiàn),在提交不同格式的數(shù)據(jù)時(shí)最大的不同點(diǎn)在于數(shù)據(jù)的組織形式不同,同時(shí)需要設(shè)置不同格式對(duì)應(yīng)的Content-type格式
package mainimport ("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()//以上數(shù)據(jù)組織完成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 JsonpostFile() //post file }總結(jié)
以上是生活随笔為你收集整理的HTTP中post方法提交不同格式的数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: HTTP中response响应数据获取
- 下一篇: 消息队列基本知识