1、http的基本请求
package mainimport ("bytes""fmt""io""net/http""net/url"
)func post(){r, err := http.Post("http://httpbin.org/post", "", nil)if err != nil {fmt.Println("ss")}defer r.Body.Close()content, err := io.ReadAll(r.Body)if err != nil {fmt.Println("ss")}fmt.Printf("%s", content)
}func get(){r, err := http.Get("http://httpbin.org/get")if err != nil {fmt.Println("ss")}defer r.Body.Close()content, err := io.ReadAll(r.Body)if err != nil {fmt.Println("ss")}fmt.Printf("%s", content)
}//http只提供了get和post的基本请求,其他的情况不存在,因此,需要自己发起请求,构造方法
func put(){requst, err := http.NewRequest(http.MethodPut, "http://httpbin.org/put", nil)if err != nil {fmt.Println("ss")}r, err := http.DefaultClient.Do(requst)if err != nil {fmt.Println("ss")}defer r.Body.Close()content, err := io.ReadAll(r.Body)if err != nil {fmt.Println("ss")}fmt.Printf("%s", content)}func deletets(){requst, err := http.NewRequest(http.MethodDelete, "http://httpbin.org/delete", nil)if err != nil {fmt.Println("ss")}r, err := http.DefaultClient.Do(requst)if err != nil {fmt.Println("ss")}defer r.Body.Close()content, err := io.ReadAll(r.Body)if err != nil {fmt.Println("ss")}fmt.Printf("%s", content)}
其中地址http://httpbin.org 是国外提供的一个验证http请求的网址,可以通过该网站进行测试。