Symfony2 REST – reading data from PUT request

Symfony2 REST – reading data from PUT request ©

I know that this is not so advanced theme to write about, but let me write few articles about little things that can bother developers and take time for searching and googling.

This article is mostly general php and not strictly Symfony2, but sometimes you will find this useful in your project.

Just to be clear: there is native Symfony 2 way to do that with using:


and I am going to explain other possibilities here…

When creating REST web service you will need sometimes to get data from PUT request.

To send the data via PUT request on client side, you will fill request’s body with data you need to send.

For example, inside routing.yml:


pattern:  /v1/user/change_password
defaults: { _controller: SurgeworksApiBundle:Users:userChangePassword}
_method:  PUT

For example, client should send several parameters to this route:
username, password, and so on …

We now need to take this parameters from request inside our controller, proccess them and return the result.

public function userChangePasswordAction($appname) {
$put_str = $this->getRequest()->getContent();
parse_str($put_str, $_PUT);
if (!isset($_PUT['username'])) {
//do something ...

Of course, there is something behind that in php:

$put_str = file_get_contents('php://input');
parse_str($put_str, $_PUT);

I hope that this little article will be helpful to you.
Cheers 🙂

You made it all the way down here so you must have enjoyed this post! You may also like:

Consuming Magento REST service using Zend_OAuth_Consumer Darko Goles
Darko Goles, | 45

Consuming Magento REST service using Zend_OAuth_Consumer

How to configure Magento REST and oAuth settings Darko Goles
Darko Goles, | 26

How to configure Magento REST and oAuth settings

Symfony2 REST Darko Goles
Darko Goles, | 7

Symfony2 REST


  1. The Symfony HttpFoundation Request object does this automatically for PUT, PATCH, and DELETE requests if the request has a Content-Type header of application/x-www-form-urlencoded: it places the parameters in the “request” ParameterBag (same place the POST params go).

    As Geo pointed out, if you’re submitting form data in a PUT request, you should be setting the Content-Type header in the request because the expected content of a PUT request (from the point of view of the HTTP standard) is a resource (an HTML page, an image, etc etc).

  2. Damien, I would be curious to know what you mean by “otherwise the content of the request is expected to be an entity”. I am trying to serve a PUT request without this header, but I am currently unable to read the request contents. Any ideas?

  3. In a REST webservice, you should only use parse_str() if the Content-Type of the request is “application/x-www-form-urlencoded”. Otherwise the content of the request is expected to be an entity.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.