The net/ HTTP package in Golang provides encapsulation of HTTP-related operations. The GET and POST methods are further encapsulated to make it easier to use. Other request methods require us to call the underlying implementation ourselves
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", / / "the 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": // "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": // "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": // "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() }