jacwright / RestServer

A PHP REST server for providing a very light-weight REST API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PUT requests not working?

TheRealGuru opened this issue · comments

Hi, maybe the documentation is unclear not sure. However my put request is just not working.


    /**
     * Marks a game as completed
     *
     *
     * @url PUT /game/end
     */
    public function endGame($data) {
        print_r($data);
    }

This just won't work, the data variable contains absolutely nothing. I've tried to via a PHP curl request and a linux curl request.

PHP Request

        $data = array('test' => 'test');
        $ch = curl_init('theurl');

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

        $response = curl_exec($ch);
        echo $response;

Linux cURL

 curl -i -H "Accept: application/json" -X PUT -d test=test url

It appears to be a bug. While the server supports multiple formats for returning data (JSON, XML, text, etc), it only accepts JSON data for POSTs and PUTs. I suppose most people just use this server for JSON and so we haven't discovered this error until now.

If you're going with JSON anyway you could just pass JSON data in and it would work. If you want to support application/x-www-form-urlencoded data you'll need to update the getData() method to parse the content type being sent and work accordingly. Pull requests welcome!

The question is if it works by specifying JSON. If it does, I'd be happy to tag this as a feature-request.