I met the RESTful ideology and I fell in love with it. However the only request methods supported by HTML forms so far are GET
and POST
, which is kind of sad, because this means that to use REST
as it's supposed to be used I need to use some work around which is likely to not be pretty, as it really is.
So the only way I found I can send PUT
or DELETE
requests is from the XhrHttpRequest
object in JavaScript.
How I send data seems pretty simple:
xhr = new XMLHttpRequest();
xhr.open('PUT' /*for example*/, URL, true);
xhr.send('MyData=HelloWorld');
Then on the other side I fetch the data:
class Input {
private static $input;
public static function init(){
parse_str(file_get_contents('php://input'), self::$input);
}
public static function data($key = null){
return $key ? self::$input[$key] : self::$input;
}
}
Input::init();
My concern is that the header Content-Type
is always text/plain;charset=UTF-8
no matter what the request type is, as opposed to using standard form submission, where Content-Type
becomes application/x-www-form-urlencoded
. I can of course put that on POST
requests as well but in this case what should I put on PUT
and DELETE
requests?
Are there any potential problems I may run into with browsers by using this nonstandard method of sending requests? Do you see any drawbacks in general, apart the one that users who don't run JavaScript will be unable to use my site?
P.S.: I was wondering, if it turned out OK to use this method, wouldn't it be more efficient and secure to use JSON
formatting to send data instead of the query string one I have shown in the example? I primarily want to avoid the query string format because I remember reading that JavaScript's URL encoding functions aren't the best.
_method=delete
. \$\endgroup\$