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
Unveiling the Potential of Quantum Computing in AI: A Game Changer for Industries
Program to print full pyramid using star
This sample program demonstrates how to create multiple goroutines and how the goroutine scheduler behaves with three logical processors.
Golang program for implementation LIFO Stack and FIFO Queue
Empowering Developers: Google Cloud’s Generative AI Systems
GO language program with example of Sort Functions for integer, strings and float64 data type
Most Helpful This Week
How to trim leading and trailing white spaces of a string in Golang?How to fix race condition using Atomic Functions in Golang?Regular Expression to get a string between parentheses in GolangHow to wait for Goroutines to Finish Execution?Replace first occurrence of string using RegexpExample: How to use TeeReader from IO Package in Golang?Select single argument from all arguments of variadic functionGolang HTML parserGenerate a Keygen of 256 bitsRegular expression to extract date(YYYY-MM-DD) from string