删除请求的正文在我的其余 api 端点中为空

Body of delete request is empty in my rest api endpoint

如果方法是 DELETE,我似乎得到了 Go http.Request 的空主体内容。但是,如果我将方法更改为 POST,那么正文内容将提供我期望的内容。

我的 golang 中的相关代码如下所示:

import(
  "github.com/gorilla/handlers"
  "github.com/gorilla/mux"
)
func Delete(w http.ResponseWriter, r *http.Request) {
  r.ParseForm()
  qs := r.Form
  log.Println(qs)
}


func main() {
  router := mux.NewRouter()

  router.HandleFunc("/profile", Delete).Methods("POST")
  router.HandleFunc("/profile", Delete).Methods("DELETE")

}

现在,当我 运行 这个 JavaScript 代码来自我的浏览器时:

fetch(sendurl,{
  method:"POST",
  headers:{
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  body:"data="+project.encodeFormURIComponent(JSON.stringify({"ids":[1032,1033]}))
})
.then(response=>{
  if(response.ok)
    return response.json();
})
.then(result=>{
  console.log(result);
})

我在我的 Golang 代码中的 qs[ids] 中看到了一组漂亮的数字。但是,如果我将 method:"POST" 更改为 JavaScript 中的 method:"DELETE",则 qs 为空。

我做错了什么?


更新

使用 DELETE 方法的 JavaScript 可以按照人们通常期望的方式填充 Go qs 变量:

fetch(sendurl+"?data="+project.encodeFormURIComponent(JSON.stringify({"ids":[1032,1033]})),{
  method:"DELETE",
  headers:{
    'Content-Type': 'application/x-www-form-urlencoded'
  }
})
.then(response=>{
  if(response.ok)
    return response.json();
})
.then(result=>{
  console.log(result);
})

所以当使用 DELETE 方法时,Go 似乎会忽略 JavaScript body 参数,但它会尊重 API 端点中的查询字符串内容 url?为什么会这样?

https://www.rfc-editor.org/rfc/rfc7231#section-4.3.5

A payload within a DELETE request message has no defined semantics; sending a payload body on a DELETE request might cause some existing implementations to reject the request.

查询字符串是请求的 target-uri 的一部分;换句话说,查询字符串是 标识符 的一部分,而不是它的附带修饰符。但是请求的消息正文是 不是 标识符的一部分。

因此您的本地框架或转发您的请求的任何其他通用组件不需要为消息正文提供支持。

想想 C 中的“未定义行为”