我在Golang项目中工作,我需要通过外部API执行一些操作:GET,PUT,POST和DELETE.目前正在使用net/http我创建的一个&http.Client{}来制作GET和PUT,这是按预期工作的.
现在我需要执行DELETE而我找不到任何关于它的信息,是否支持?我需要基本上调用这样的URL:
somedomain.com/theresource/:id
Method: DELETE
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
nba*_*ari 10
以下是如何执行此操作的一个小示例:
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func sendRequest() {
// Request (DELETE http://www.example.com/bucket/sample)
// Create client
client := &http.Client{}
// Create request
req, err := http.NewRequest("DELETE", "http://www.example.com/bucket/sample", nil)
if err != nil {
fmt.Println(err)
return
}
// Fetch Request
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
// Read Response Body
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
// Display Results
fmt.Println("response Status : ", resp.Status)
fmt.Println("response Headers : ", resp.Header)
fmt.Println("response Body : ", string(respBody))
}
Run Code Online (Sandbox Code Playgroud)