关于golang:HTTP的四种请求方法

60次阅读

共计 2324 个字符,预计需要花费 6 分钟才能阅读完成。

golang 中 net/http 包提供了 http 相干操作的封装,其中 get 办法和 post 办法进行的进一步的封装,应用起来更加不便,其余的申请形式须要咱们本人调用底层实现

package main

import (
   "fmt"
   "io/ioutil"
   "net/http"
)

func get(){resp, err := http.Get("http://httpbin.org/get")
   if err != nil {panic(err)
   }
   defer func() {_ = resp.Body.Close()}()

   content, err := ioutil.ReadAll(resp.Body)
   if err != nil {panic(err)
   }
   fmt.Printf("%s", content)
   //{// "args": {},
   // "headers": {
   // "Accept-Encoding": "gzip",
   //    "Host": "httpbin.org",
   //    "User-Agent": "Go-http-client/1.1",
   //    "X-Amzn-Trace-Id": "Root=1-60e4663e-4a475772249555e35a89632c"
   //},
   // "origin": "222.211.214.252",
   // "url": "http://httpbin.org/get"
   //}
}

func post(){resp, err := http.Post("http://httpbin.org/post", "", nil)
   if err != nil {panic(err)
   }
   defer func() {_ = resp.Body.Close()}()

   content, err := ioutil.ReadAll(resp.Body)
   if err != nil {panic(err)
   }
   fmt.Printf("%s", content)
   //{// "args": {},
   // "data": "",
   // "files": {},
   // "form": {},
   // "headers": {
   // "Accept-Encoding": "gzip",
   //    "Content-Length": "0",
   //    "Host": "httpbin.org",
   //    "User-Agent": "Go-http-client/1.1",
   //    "X-Amzn-Trace-Id": "Root=1-60e466bc-19f2a05e219847055d72f159"
   //},
   // "json": null,
   // "origin": "222.211.214.252",
   // "url": "http://httpbin.org/post"
   //}
}

func put(){request, err := http.NewRequest(http.MethodPut, "http://httpbin.org/put", nil)
   if err != nil {panic(err)
   }
   resp, err := http.DefaultClient.Do(request)
   if err != nil {panic(err)
   }
   defer func() {_ = resp.Body.Close()}()

   content, err := ioutil.ReadAll(resp.Body)
   if err != nil {panic(err)
   }
   fmt.Printf("%s", content)
   //{// "args": {},
   // "data": "",
   // "files": {},
   // "form": {},
   // "headers": {
   // "Accept-Encoding": "gzip",
   //    "Content-Length": "0",
   //    "Host": "httpbin.org",
   //    "User-Agent": "Go-http-client/1.1",
   //    "X-Amzn-Trace-Id": "Root=1-60e467e5-4db5430f5a0aa8ea75f5f805"
   //},
   // "json": null,
   // "origin": "222.211.214.252",
   // "url": "http://httpbin.org/put"
   //}
}

func delete(){request, err := http.NewRequest(http.MethodDelete, "http://httpbin.org/delete", nil)
   if err != nil {panic(err)
   }
   resp, err := http.DefaultClient.Do(request)
   if err != nil {panic(err)
   }
   defer func() {_ = resp.Body.Close()}()

   content, err := ioutil.ReadAll(resp.Body)
   if err != nil {panic(err)
   }
   fmt.Printf("%s", content)
   //{// "args": {},
   // "data": "",
   // "files": {},
   // "form": {},
   // "headers": {
   // "Accept-Encoding": "gzip",
   //    "Host": "httpbin.org",
   //    "User-Agent": "Go-http-client/1.1",
   //    "X-Amzn-Trace-Id": "Root=1-60e4683b-6e4e08c400343a9f29a735fe"
   //},
   // "json": null,
   // "origin": "222.211.214.252",
   // "url": "http://httpbin.org/delete"
   //}
}

func main(){get()
   post()
   put()
   delete()}

正文完
 0