codeigniter-restserver icon indicating copy to clipboard operation
codeigniter-restserver copied to clipboard

DELETE method & JSON request

Open oytuntez opened this issue 14 years ago • 4 comments

Hello all,

Thanks for this precious library. It's a very nice one.

I have encountered a problem while trying to use it with DELETE request by EXTJS Rest proxy. Proxy sends a complete JSON object of the record that will be deleted. Like this:

[CODE] DELETE http://localhost/km1/admin/gateway/index.php/api/tour_gallery/4?dc=1336230707386 HTTP/1.1 Host: localhost Connection: keep-alive Content-Length: 122 Origin: http://localhost X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19 Content-Type: application/json Accept: / Referer: http://localhost/km1/admin/app.html Accept-Encoding: gzip,deflate,sdch Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4 Accept-Charset: ISO-8859-9,utf-8;q=0.7,*;q=0.3 Cookie: wp_gdsr_artthumb=voted%7C64%7C33%7C30%7C27

{"id":"4","tourid":"3","thumb":"thumb_1aad297d186cfc66dc449b216994a2ec.jpg","full":"1aad297d186cfc66dc449b216994a2ec.jpg"}

[/CODE]

However, the library failed on parsing the DELETE variables. When print_r($this->_delete_args), the result was unusable:

[CODE] Array ( [{"id":"2","tourid":"3","thumb":"thumb_b5fbe4efa7467fc7bf19f10b761f6111_png","full":"b5fbe4efa7467fc7bf19f10b761f6111_png"}] => ) [/CODE]

The key was filled with the json request with no value corresponding. So, I changed the SWITCH the __CONSTRUCT function in the library like this after trying to succeed with other methods as you can see in comments:

[CODE] case 'delete': // Set up out DELETE variables (which shouldn't really exist, but sssh!) //parse_str(file_get_contents('php://input'), $this->_delete_args); /_parse_str(file_get_contents('php://input'), $delme); $this->_delete_args = array_merge($this->delete_args, $delme);/

            if (stripos($_SERVER["CONTENT_TYPE"], "application/json")===false) {
                    //$_POST = json_decode(file_get_contents("php://input"));
                parse_str(file_get_contents('php://input'), $this->_delete_args);
                echo "json 0 - ".$_SERVER["CONTENT_TYPE"];
            }
            else
            {
                $this->_delete_args = (array)json_decode(file_get_contents("php://input"));
            }

            break;

[/CODE]

Hope this will help others. If I am doing something wrong or this was an unnecessary change, please let me know.

Thanks for the library again.

oytuntez avatar May 05 '12 15:05 oytuntez

tnx for your solution! it works in rest server 2.6.1 too :)

arrie1992 avatar Nov 24 '12 13:11 arrie1992

~Sending a body in HTTP DELETE violates the HTTP RFC, so it is not supported. If people want to hack this in they are welcome :)~

Update 30/12/2017: Sorry, I was completely wrong here. Just in case anyone finds this on Google...

Neither RFC 7231: Hypertext Transfer Protocol: Semantics and Content, nor now-obsolete RFC 2616 ever said that DELETE is not allowed to have a body, or even hinted at it. I think this is just something people often see on StackOverflow and repeat as truth, which is the modern day equivalent of "A guy in a pub said so".

RFC 7231 did add a warning that some HTTP servers and implementations will reject this, but that is a polite heads up from the HTTP crowd about something various server projects have got wrong over the years, not a failure of REST in any way…

4.3.5. DELETE

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.

Source

Sorry for being wrong about a thing for years.

philsturgeon avatar Dec 11 '12 00:12 philsturgeon

The solution by @oytuntez is not advised because:

  • Prior to PHP 5.6 "php://input" could not be read twice.
  • In codeigniter, "php://input" === $this->input->raw_input_stream (when called from a controller that extends CI_Controller).
  • REST_Controller does some pre-processing on the request body _(in it's _construct()) before setting the $this->_delete_args dynamically. It's best not to set it manually.

See this Gist for my alternative to the above hack by @oytuntez.

BTW: I'll suggest you extend the REST_Controller class and use the extended class instead of editing the REST_Controller class directly. See my Gist.

Ema4rl avatar Jun 09 '16 17:06 Ema4rl

You are welcome to do a pull request with your changes so everybody has the correct solution

fabianblum avatar Dec 30 '17 18:12 fabianblum