1

I have two different end points. One is

sentPost: function() {
  axios
    .post("/article", {
      title: this.title,
      content: this.editorContent
    }) // axios.<method>(url, {<data>})
    .then(({ data }) => {
      Swal.fire("Success!", "Article Posted!", "success");
      this.$emit("pagecontrol", "explore");
    })
    .catch(err => {
      let fields = err.response.data.join(" | ");
      Swal.fire({
        icon: "error",
        title: "Oops...",
        text: fields
      });
    });
}

and the other one is

destroy: function(article) {
  let id = article._id;
  axios
    .delete("/article", {
      data: {
        id
      }
    })
    .then(({ data }) => {
      Swal.fire("Success!", "Article Deleted!", "success");
    })
    .catch(err => {
      let fields = err.response.data.join(" | ");
      Swal.fire({
        icon: "error",
        title: "Oops...",
        text: fields
      });
    });
}

Why in the second one do I have to declare "data" in the axios config. I mean, in the first one I don't need to declare "data" but I still can access req.body from it.

I used to code axios config like this.

axios.get(url, {
  data
})

I never had an error with that before, but now I need to declare "data".

cokeman19
  • 2,405
  • 1
  • 25
  • 40

1 Answers1

0

I think you should not send a body in DELETE. You should use it like GET, and pass parameters in the URL.

Read: Axios API, DELETE body

Request method aliases:

axios.post(url[, data[, config]])
axios.delete(url[, config])

Request Config:

{
...
  // `data` is the data to be sent as the request body
  // Only applicable for request methods 'PUT', 'POST', and 'PATCH'
  // When no `transformRequest` is set, must be of one of the following types:
  // - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
  // - Browser only: FormData, File, Blob
  // - Node only: Stream, Buffer
  data: {
    firstName: 'Fred'
  },
...
}
Gander
  • 1,854
  • 1
  • 23
  • 30