1

I am building an API with Apigility. I am tied to a backend, where deleting a resource requires additional data, not just the resource ID taken from the URL. So I need to read a parameter fom a DELETE request's body.

While in POST request's create() method I can simply access the $data parameter, this does not work with DELETE methods, because only $id is provided.

Accessing $this->getEvent()->getRequest(); in my Resource classes' delete() method, I see the request's body content (form-data) wrapped into a property called 'content' - as a string.

Can someone point me at what I am missing to access the body's key-value pairs?

hennzen
  • 595
  • 5
  • 15

1 Answers1

1

Apigility does not expect any data to be passed to a DELETE request, so it does not pass it to the event params. You can just retrieve it from the request as you discovered and do the json_decode yourself.

public function delete($id)
{
    $body = $this->getEvent()->getRequest()->getContent();
    $data = json_decode($body, true);
}
Bram Gerritsen
  • 7,178
  • 4
  • 35
  • 45
  • Thanks for the reply. Unfortunately `$body` is not valid JSON but a string like the following, `var_dump()`ed: `string '------WebKitFormBoundaryltcFOyHq6kVAGS1X Content-Disposition: form-data; name="device_type" android ------WebKitFormBoundaryltcFOyHq6kVAGS1X-- ' (length=149)` I am using Postman to send the DELETE request, using its form-data fields. – hennzen Jul 06 '15 at 15:42
  • 1
    Problem solved: the solution was in Postman to set the header `Content_type: application/json` and send the request with raw body, containing the JSON string, e.g. `{ "device_type": "android" }`. This way the above solution works perfectly. Thanks! – hennzen Jul 07 '15 at 08:10
  • Cannot upvote the answer due to a lack of reputation, alas. – hennzen Jul 07 '15 at 08:12