Does Luracast Restler support the multipart / form-data format?

advertisements

I have a sample Restler class:

   class Author {
      ....

      function post($request_data=NULL) {
         var_dump($request_data);
         var_dump($_FILES);
         var_dump($_REQUEST);

         return $this->dp->insert($this->_validate($request_data));
       }

      ....
  }

I'm trying to POST file and some data to Restler service by simple HTML form:

  <FORM action="http://host/index.php/author" enctype="application/x-www-form-urlencoded" method="post">
      Name:  <INPUT type="text" name="name" value="dima"><BR>
      Email: <INPUT type="text" name="email" value="[email protected]"><BR>
      File:  <INPUT type="file" name="files"><BR>
      <INPUT type="submit" value="Send"> <INPUT type="reset">
  </FORM>

It is clear, that the $_FILES array will be empty, but $_REQUEST and $request_data will have three variables: name = "dima", email = "[email protected]" and file = "selected file name".

In the next test, I changed the form enctype value to multipart/form-data.

  <FORM action="http://host/index.php/author" enctype="multipart/form-data" method="post">
      Name:  <INPUT type="text" name="name" value="dima"><BR>
      Email: <INPUT type="text" name="email" value="[email protected]"><BR>
      File:  <INPUT type="file" name="files"><BR>
      <INPUT type="submit" value="Send"> <INPUT type="reset">
  </FORM>

When I press submit form, in the $_REQUEST array, I will see the same three variables, the $_FILES array will be filled with uploaded file information, BUT the $request_data array will be empty!!

Can anybody help in this situation? Where have I made a mistake?


I have solved this issue, by writing Restler plugin, may be other will have the same problem:

<?php

/**
 * URL Encoded String in multipart data format
 * @category   Framework
 * @author     Dmitrij Orlov <[email protected]>
*/
class UrlMultipartFormat implements iFormat
{
    const REQUEST_MIME      = 'multipart/form-data';
    const RESPONCE_MIME     = 'application/json';
    const EXTENSION         = 'post';

    public function getMIMEMap() {
        return array(self::EXTENSION=>self::REQUEST_MIME);
    }

    public function getMIME(){
        return self::RESPONCE_MIME;
    }

    public function getExtension(){
        return self::EXTENSION;
    }

    public function setMIME($mime){
        //do nothing
    }

    public function setExtension($extension){
        //do nothing
    }

    public function encode($data, $human_readable=FALSE){
        return $human_readable ?
        $this->json_format(json_encode(object_to_array($data))) :
        json_encode(object_to_array($data));
    }

    public function decode($data){
        return $_REQUEST;
    }

    public function __toString(){
        return $this->getExtension();
    }

    /**
     * Pretty print JSON string
     * @param string $json
     * @return string formated json
     */
    private function json_format($json)
    {
        $tab = "  ";
        $new_json = "";
        $indent_level = 0;
        $in_string = FALSE;

        $len = strlen($json);

        for($c = 0; $c < $len; $c++) {
            $char = $json[$c];
            switch($char) {
                case '{':
                case '[':
                    if(!$in_string) {
                        $new_json .= $char . "\n" .
                        str_repeat($tab, $indent_level+1);
                        $indent_level++;
                    } else {
                        $new_json .= $char;
                    }
                    break;
                case '}':
                case ']':
                    if(!$in_string) {
                        $indent_level--;
                        $new_json .= "\n".str_repeat($tab, $indent_level).$char;
                    } else {
                        $new_json .= $char;
                    }
                    break;
                case ',':
                    if(!$in_string) {
                        $new_json .= ",\n" . str_repeat($tab, $indent_level);
                    } else {
                        $new_json .= $char;
                    }
                    break;
                case ':':
                    if(!$in_string) {
                        $new_json .= ": ";
                    } else {
                        $new_json .= $char;
                    }
                    break;
                case '"':
                    if($c==0){
                        $in_string = TRUE;
                    }elseif($c > 0 && $json[$c-1] != '\\') {
                        $in_string = !$in_string;
                    }
                default:
                    $new_json .= $char;
                    break;
            }
        }

        return $new_json;
    }

}

?>