HTTP DELETE body

697 Views Asked by At

I need to send DELETE request in my backend with data in body, but I got error when sending it. I want to delete row that matches data I'm sending in that body. In project spec. is defined, that I need to delete record in DB with DELETE request with body that contains record data.

This is old version of api (not created by me), and I need to do exactly the same thing.

DELETE /api/display/event/:id  - id is display identificator
BODY:
{
    "eventId": "AAMkADkxNGMzMTlmLTVhMTItNGYyNS ... AA=",
    "organizer": "Lukas Racek",
    "subject": "",
    "reservationStart": "1580293800",
    "reservationEnd": "1580295600"
}

Example request in specification:

curl --location --request DELETE '/api/display/event/:id' --header 'Content-Type: application/json' --data-raw '{"eventId": "AAMkADkxNGMzMTlmLTVhMTItNGYyNS1 ... AA=,"organizer": "Lukas Racek","subject": "","reservationStart": "1580293800","reservationEnd": "1580295600"}'

This is method I created, but is not working:

 async remove(id, data) {
    try {
      const { eventId, organizer, subject, reservationStart, reservationEnd } = data
      const deletedRecords = await this.Model('reservations_ms')
        .where({
          event_id: eventId,
          event_organizer: organizer,
          event_subject: subject,
          event_start: reservationStart,
          event_end: reservationEnd
        })
        .delete()

      if (deletedRecords === 0) {
        throw new Error('No matching reservations found')
      }

      return { status: true }
    } catch (error) {
      console.error(error)
      return { status: false }
    }
  }

Error I got:

Error: Undefined binding(s) detected when compiling DEL. Undefined column(s): [event_id, event_organizer, event_subject, event_start, event_end] query: delete from [reservations_ms] where [event_id] = ? and [event_organizer] = ? and [event_subject] = ? and [event_start] = ? and [event_end] = ?;select @@rowcount

I know, that DELETE request works with params in URL, but my boss says, that it is possible to make DELETE request with body. How to send that body in delete request?

Edit: It works like this

    async remove(id, params) {
    try {
      const { eventId, organizer, subject, reservationStart, reservationEnd } = params
      // console.log('Received DELETE request with data:', data)

      const deletedRecords = await this.Model('reservations_ms')
        .where({
          event_id:
            'AAMkAGRkMWMwNmY5LTNkZmEtNDQ0OC04N2M0LTJkOTNiMzBlYTU2NABGAAAAAAD2BgqheO9gQJafkFrwWOt1BwD8CHYsQ6RTSJbwlx49v1bzAAAAAAENAAD8CHYsQ6RTSJbwlx49v1bzAAALw05SAAA=',
          event_organizer: '[email protected]',
          event_subject: 'Príjemný - ničím nerušený utorok',
          event_start: '2023-10-03T08:00:00.0000000',
          event_end: '2023-10-03T18:00:00.0000000'
        })
        .delete()

      if (deletedRecords === 0) {
        throw new Error('No matching reservations found')
      }

      return { status: true }
    } catch (error) {
      console.error(error)
      return { status: false }
    }
}

But I'm still not able to do it when I send these data in request in postman.

1

There are 1 best solutions below

0
Quentin On

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.

the spec

A DELETE request should delete the thing represented by the URL. You shouldn’t need a request body because all the information needed to identify the thing you want to delete should be in the URL (I.e. it should be something you could make a GET request for).