How do you send an HTTP DELETE request in Go?
HTTP DELETE is a method used in the HTTP protocol for sending a request to a server to delete a specified resource. It is one of the most commonly used HTTP methods along with GET, POST, PUT, and PATCH. The DELETE method is used to delete the resource identified by the Request-URI, which can be a specific resource, a collection of resources, or even an entire object. When a DELETE request is sent to the server, the server will remove the specified resource or collection of resources, and return a response to indicate whether the operation was successful or not.
HTTP DELETE request
To send an HTTP DELETE request in Go, you can use the net/http package, which provides a convenient http.NewRequest function to create a new HTTP request with the specified method, URL and body. Here's an example of how to send an HTTP DELETE request in Go:
Example
package main
import (
"fmt"
"net/http"
"io/ioutil"
)
func main() {
// create a new HTTP client
client := &http.Client{}
// create a new DELETE request
req, err := http.NewRequest("DELETE", "http://example.com/api/resource/123", nil)
if err != nil {
panic(err)
}
// send the request
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// print the response body
fmt.Println(string(body))
}
HTTP DELETE request using GIN
To send an HTTP DELETE request in Go Gin, you can use the DELETE method of the gin.Context object. The DELETE method takes a URL path and a handler function as arguments. Inside the handler function, you can use the context.Request object to create a new HTTP request with the DELETE method, and then send it using the http.Client object.Here's an example of how to send an HTTP DELETE request in Go Gin:
Example
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
// create a new router
r := gin.Default()
// define a DELETE route
r.DELETE("/api/resource/:id", func(c *gin.Context) {
// get the resource ID from the URL parameters
id := c.Param("id")
// create a new HTTP client
client := &http.Client{}
// create a new DELETE request with the resource ID in the URL path
req, err := http.NewRequest("DELETE", "http://example.com/api/resource/"+id, nil)
if err != nil {
panic(err)
}
// send the request
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// set the response status code
c.Status(resp.StatusCode)
})
// start the server
r.Run(":8080")
}
HTTP DELETE request using Beego
To send an HTTP DELETE request in Go Beego, you can use the Del method of the beego.Controller object. The Del method takes a URL path as an argument and sends a DELETE request to that URL.Here's an example of how to send an HTTP DELETE request in Go Beego:
Example
package main
import (
"github.com/astaxie/beego"
"net/http"
)
type MyController struct {
beego.Controller
}
func (c *MyController) Delete() {
// get the resource ID from the URL parameters
id := c.Ctx.Input.Param(":id")
// create a new HTTP client
client := &http.Client{}
// create a new DELETE request with the resource ID in the URL path
req, err := http.NewRequest("DELETE", "http://example.com/api/resource/"+id, nil)
if err != nil {
panic(err)
}
// send the request
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// set the response status code
c.Ctx.ResponseWriter.WriteHeader(resp.StatusCode)
}
func main() {
// create a new controller
c := &MyController{}
// define a DELETE route
beego.Router("/api/resource/:id", c, "delete:Delete")
// start the server
beego.Run(":8080")
}
In this example, we define a new controller with a Delete method, which sends a DELETE request to the specified URL path. We also define a DELETE route with the beego.Router method and bind it to the Delete method of the MyController controller.
Inside the Delete method, we get the resource ID from the URL parameters, create a new HTTP client, and send a DELETE request with the resource ID in the URL path using the http.NewRequest and http.Client objects. We set the response status code using the c.Ctx.ResponseWriter.WriteHeader method of the beego.Controller object.
Most Helpful This Week
How do you handle HTTP redirects in Go?
How do you set headers in an HTTP response in Go?
What is an HTTP client in Go?
What is the default HTTP server in Go?
How do you handle HTTP Client server load balancing in Go?
How do you handle HTTP errors in Go?
How do you handle HTTP client caching in Go?
How do you send an HTTP POST request with an HTTP client in Go?
How do you handle HTTP client server security in Go?
How do you set headers in an HTTP request with an HTTP client in Go?
Most Helpful This Week
Regular expression to validate email addressRegular expression to extract all Non-Alphanumeric Characters from a StringHow to create Map using the make function in Go?How to convert Boolean Type to String in Go?Golang String ConcatenationHow to reads and decodes JSON values from an input stream?Golang download image from given URLHow to count number of repeating words in a given String?How to print string with double quote in Go?How to import structs from another package in Go?