[英]access HTTP PUT data in Symfony2
Via HTTP PUT I send the following json to my webservice, verified by return new Response($request->getContent());
通过HTTP PUT我将以下json发送到我的webservice,通过
return new Response($request->getContent());
: :
{"company_id":13}
In my webservice I am trying to retrieve the data by its tag from the request: 在我的web服务中,我试图通过请求中的标记检索数据:
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? symfony2书只提到如何从GET和POST获取数据,如何从PUT请求中检索数据?
You are getting it from $request->getContent()
, just json_decode
it and you should get an object, so you can access it then. 你从
$request->getContent()
得到它,只是json_decode
它,你应该得到一个对象,所以你可以访问它。 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. Symfony Http Foundations
get
方法基本上只是$request->attributes->get
, $request->query->get
和$request->request->get
的“别名”,所以如果一个返回0或false,或者无论如何,很可能另一个也会。
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,...). 由于HTTP PUT将数据作为主体发送,因此Request对象不会尝试以任何方式解码它,因为它可能有多种不同的格式(JSON,XML,非标准,......)。 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. 如果您希望通过
get
方法调用来访问它,则必须手动解码它,然后将其添加到其request
或query
属性中。
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. HTTP协议对JSON / XML /序列化等等一无所知。
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). 正如@slaxOr所说,你必须从请求中获取并自己解码(可能有捆绑包为你做,但我不知道它们)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.