Apigility return error even after successful action

I am creating a REST API using Apigility with Zend Framework 2. In this API, I have a code-related REST service, and I encounter strange behavior when I try to delete an object. I use the delete method of my TableGateway object to delete the data passed to the delete method of the Resource file:

public function delete($id)
{
    //GetTable returns a TableGateway instance
    $this->getTable('order')->delete(array('id' => $id));
    return array("status" => "deleted", "id" => $id);
}

I tested this feature using the Postman REST client and got the answer:

{
    "type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
    "title":"Unprocessable Entity",
    "status":422,
    "detail":"Unable to delete entity."
}

However, when I checked the mysql database, the object in question was deleted correctly. There were no signs of error.

What could be the reason for this error?

Update: The code displays the lines after calling the delete function of the TableGateway function. This means that the answer is probably built after the function call, and the return value that I return is ignored.

+4
3

"return true", API HTTP 204 .

...
class ItemResource extends AbstractResourceListener
{
    ...
    public function delete($id)
    {
        $service = $this->serviceManager->get('...\ItemService');
        $service->deleteItem($id);
        return true;
    }
    ...
}
+11

, , . , , , . , , ZF2 API DELETE.

, , , " ", .

+1

You are mistaken in performing the deletion .. but in the wrong answer that you are transmitting.

After deleting, you need to think about how to correctly confirm this in a well-formed (likely JSON-based) response object.

-1
source

Source: https://habr.com/ru/post/1533090/


All Articles