HTTP的四种请求方法

这篇博客展示了如何使用Golang的net/http包进行GET、POST、PUT和DELETE请求。通过示例代码,我们可以看到http.Get和http.Post的便捷用法,而PUT和DELETE则需要创建Request对象并使用DefaultClient.Do来执行。每个请求的响应内容被读取并打印出来,显示了请求的相关头部信息。
摘要由CSDN通过智能技术生成

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()
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值