PUT request body params


#1

I’m having troubles getting the body params parsed from a PUT request.
I’ve read some issues/pull on github, but finally I got no solution.
Any workaround to get this “fixed”? Shoul I set any specific content type
or somehow simulate a POST? Many thanks in advance.

I’m using latest slim.


#2

Do you have an example?

Have you tried this?


#3

I’ve tried setting content type to: application/x-www-form-urlencoded, then:

$bodyParams = $request->getParsedBody();
var_dump($bodyParams[‘myParam’]);

but I cannot access myParam inside the array, because it is sorrounded with encoding…


#4

Aha you are trying the get POST variables from a PUT request.

In Slim 3 the POST variables are only parsed if the request

  1. method is POST and
  2. the media type is: ‘application/x-www-form-urlencoded’ or ‘multipart/form-data’.

Let’s try this (not tested):

$parsedBody = $request->getParsedBody();
parse_str($parsedBody, $postData);

print_r($postData);

PS: According to the HTTP specification RFC 7231 (Section 4.3.4), the PUT request can have a payload. Therefore, it must be parsed in the slim Request implementation. Looks like a bug to me.

More infos:
https://tools.ietf.org/html/rfc7231#section-4.3.4


#5

Looks like a bug to me too.
Your solution is nearly ok, but return an empty array, meanwhile I found this workaround:

parse_str(file_get_contents(‘php://input’), $bodyParams);

is this accetable/secure? Many thanks for your time.


#6

Slim is a PSR-11 based micro framework. So you must use the Request object and not the vanilla php functions.

e.g.

parse_str($request->getBody()->getContents(), $bodyParams);

Question: Why don’t you send JSON via PUT?