Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to retrieve parameters from PUT method #203

Open
matthewgovaere opened this issue Apr 16, 2014 · 4 comments
Open

Unable to retrieve parameters from PUT method #203

matthewgovaere opened this issue Apr 16, 2014 · 4 comments

Comments

@matthewgovaere
Copy link

Unable to retrieve parameters from the method 'PUT' using $request->params(). I'm assuming that the 'DELETE' method has the same issue.

Am I missing something? If not, I may be able to help add it in.

@matthewgovaere matthewgovaere changed the title Unable to retrieve parameter from PUT method Unable to retrieve parameters from PUT method Apr 16, 2014
@ada
Copy link

ada commented May 11, 2014

I have no problem sending data using PUT. Check the examples. Are you trying to retrive JSON data or form data?

@matthewgovaere
Copy link
Author

@alexanis, I'm sending a curl request (with a PUT method) to a server running Klein. When I try to retrieve the parameters using $request->params(), they're not there. They are available if I use parse_str(file_get_contents('php://input'), $put_vars); instead though.

@Rican7 Rican7 self-assigned this May 14, 2014
@Rican7
Copy link
Member

Rican7 commented May 14, 2014

@matthewgovaere The parameters aren't automatically parsed from the request body using in Klein. $request->params() is a method to get the combination of parameters from multiple sources (including the GET and POST params).

Since request body's can have multiple formats/content-types (multipart/form-data, application/json, application/x-www-form-urlencoded, etc.) the Klein library doesn't try to parse the parameters for you based on the content-type. However, PHP will do this when using multipart/form-data (what goes in the $_POST var), so the library will use those parameters by default.

I may add parameter parsing by default, but since they can have a pretty opinionated parsing process I may make it optional or configurable somehow. Until then, you'll have to parse the parameters yourself.

@funkytaco
Copy link

funkytaco commented Oct 29, 2016

I put this code (found via SO) in my base controller class.

public function http_put_data(\stdClass &$a_data) {
      $input = file_get_contents('php://input');
      preg_match('/boundary=(.*)$/', $_SERVER['CONTENT_TYPE'], $matches);
      if ($matches) {
          $boundary = $matches[1];
          $a_blocks = preg_split("/-+$boundary/", $input);
          array_pop($a_blocks);
      } else {
          parse_str($input, $a_blocks);
      }

      foreach ($a_blocks as $id => $block) {
        if (empty($block))
          continue;


        if (strpos($block, 'application/octet-stream') !== FALSE) {
          preg_match("/name=\"([^\"]*)\".*stream[\n|\r]+([^\n\r].*)?$/s", $block, $matches);
        }
        else {
          preg_match('/name=\"([^\"]*)\"[\n|\r]+([^\n\r].*)?\r$/s', $block, $matches);
        }
        if ($matches) {
            $a_data->{$matches[1]} = $matches[2];
        } else {
            $a_data->{$id} = $block;
        }
      }
    }

Then you access the data like so.

$put_data = (object)[];
$this->http_put_data($put_data);
echo $put_data->someVar;

EDIT: I had to make a change to handle empty $matches. I also made an $a_data object instead of array, as I like object syntax better for this to match Klein's style.

This probably doesn't handle every data type you can PUT. It works perfectly fine for form-data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants