tonic icon indicating copy to clipboard operation
tonic copied to clipboard

DELETE body request ?

Open hugsbrugs opened this issue 10 years ago • 2 comments

Hi and thanks for your API !

I'm wondering why I can't send DELETE entity body params but only URL params. When trying to do so I get a 415 (Unsupported Media Type).

Googling around makes me think it should be possible, could you please highlight me on the subject ?

The reason for this is when I want to delete a user I send its id but I also want to send a delete reason (I could add a second param but as it is text, I feel it would be a cleaner solution to send it through body ...)

Hugo

hugsbrugs avatar Apr 02 '15 09:04 hugsbrugs

Hi Hugo,

You should be able to send a request body for any method type, and your use-case seems to be a totally valid one.

Ensure that your defined resource is configured to allow it with the correct content type (@accepts).

If you're still having problems, stick some more info into of your specific case into this issue or create a test case that fails in a pull request.

Paul.

peej avatar Apr 03 '15 15:04 peej

Hey Paul !

I've made it but what is strange is I had to remove @accepts line !! Quite weird ! For informational purpose, here is how I get PUT, GET, POST & DELETE requests works :

/**
 * @uri /website
 */
class WebsiteApi extends Tonic\Resource
{
    /**
     * @method GET
     * @json
     * @provides application/json
     * @return Tonic\Response
     */
    function get()
    {
        error_log('WebsiteApi GET : ' . json_encode($this->request->data));
        return new Tonic\Response(Tonic\Response::OK, array('status' => 'success'));
    }

    /**
     * @method POST
     * @json
     * @provides application/json
     * @return Tonic\Response
     */
    function create()
    {
        error_log('WebsiteApi POST : ' . json_encode($this->request->data));
        return new Tonic\Response(Tonic\Response::OK, array('status' => 'success'));
    }

    /**
     * @method PUT
     * @json
     * @provides application/json
     * @return Tonic\Response
     */
    function update() {
        error_log('WebsiteApi PUT : ' . json_encode($this->request->data));
        return new Tonic\Response(Tonic\Response::OK, array('status' => 'success'));
    }

    /**
     * @method DELETE
     * @json
     * @provides application/json
     * @return Tonic\Response
     */
    function remove()
    {
        error_log('WebsiteApi DELETE : ' . json_encode($this->request->data));
        return new Tonic\Response(Tonic\Response::OK, array('status' => 'success'));
    }

    /**
     * Process all request's data as json : encode / decode
     */
    function json()
    {
        $this->before(function ($request)
        {
            if ($request->contentType == "application/json")
            {
                $request->data = json_decode($request->data);
            }
        });
        $this->after(function ($response)
        {
            $response->contentType = "application/json";
            $response->body = json_encode($response->body);
        });
    }
}

By the way, I have to tell you In your readme, you provide an example with a list() function : that throws me an error (at least with my version of PHP) : you can't use reserved words ...

hugsbrugs avatar Apr 13 '15 15:04 hugsbrugs