6

Via HTTP PUT I send the following json to my webservice, verified by return new Response($request->getContent());:

{"company_id":13}

In my webservice I am trying to retrieve the data by its tag from the request:

var_dump("COMPANY ID ".$request->request->getInt('company_id')); //returns 0

I've also tried:

//the 2 below should only work on GET from what I read
var_dump("COMPANY ID ".$request->get('company_id')); //returns nothing
var_dump("COMPANY ID ".$request->query->getInt('company_id')); //returns 0

The symfony2 book only mentions how to get data from GET and POST, how do I retrieve data from a PUT request?

2 Answers 2

7

You are getting it from $request->getContent(), just json_decode it and you should get an object, so you can access it then. Example:

$data = json_decode($request->getContent());
var_dump("COMPANY ID " . $data->company_id);

Edit to add a little bit more explanation.

Symfony Http Foundations get method is basically just an "alias" for $request->attributes->get, $request->query->get, and $request->request->get, so if one returns 0 or false, or whatever, quite probably the other will too.

Since HTTP PUT sends data as body, the Request object does not try to decode it in any way, because it could have been in multiple different formats(JSON, XML, non-standard,...). If you wish to access it through get method call, you will have to decode it manually and then add it to its request or query properties.

Sign up to request clarification or add additional context in comments.

4 Comments

Wow, that's pretty counter intuitive but it works. Life-saver, will accept answer as soon as the cooldown wears off. What kind of object is $data though? Does symfony2 have some sort of special object for decoded json? I now understand that the http content is seen as a single string.
No, json_decode will return a standard object, or you can ask it to return an array. Please see php.net/json_decode
I was wondering in case I want to use a more complex json. For example $data->company->employees->43; or should I just treat it like a multi-dimensional array? I'll read up on what a stdObject is.
If you wish to create a more complex object out of your JSON, you may create a class that will parse this JSON. And afaik PHP can not handle ->43, numerical object properties.
1

You will never get it as a single parameter, because it is not a parameter. It's a raw content that you have to decode yourself.

HTTP protocol does not know anything about JSON / XML / serializing / whatever.

It only sees a text string.

As @slaxOr said, you'll have to get from the request and decode it yourself (there may be bundles that do it for you, but I am not aware of them).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.