[英]Slim PUT request empty body
I am using slim php framework for developing REST API. 我正在使用slim php框架来开发REST API。 I am successful in implementing POST and GET requests. 我成功实现了POST和GET请求。 I am using ContentTypes middleware as well to parse the JSON body in POST and PUT requests however my PUT request always gives empty string on the server. 我也在使用ContentTypes中间件来解析POST和PUT请求中的JSON主体但是我的PUT请求总是在服务器上提供空字符串。 POST just works fine and I can get the parsed JSON as PHP associative array but cant get it in PUT request. POST工作正常,我可以将解析后的JSON作为PHP关联数组但不能在PUT请求中获取它。 I am using application/json in headers and I dont want to use application/x-www-form-urlencoded method. 我在头文件中使用application / json,我不想使用application / x-www-form-urlencoded方法。
$app->map('/example/:id', function ($id) use($app, $log) {
//$body = $app->request()->getBody();
//using the above in other POST calls & it works but does not in this case
$body = json_decode($app->request()->getBody()); //tried this. no success
var_dump($body);
} )->via ( 'PUT', 'PATCH' );
I am calling it via CURL like this 我这样通过CURL调用它
$headers = array(
'Content-Type'=>'application/json;charset=utf-8',
);
$id = 123;
$body = array("name"=>"myfirstname","email"=>"myemail");
$json_str = json_encode($body);
$response = Requests::put($base_url.'/api/v1/example/'.$id,$headers,$json_str);
When I try to return the same JSON from the API it returns empty array. 当我尝试从API返回相同的JSON时,它返回空数组。 I tried POSTMAN on chrome and above code but does not work. 我在chrome和上面的代码上尝试过POSTMAN但是不起作用。 What is the issue. 问题是什么
Update: I have verified the same code works on localhost but does not work on remote dev server. 更新:我已验证相同的代码适用于localhost但在远程开发服务器上不起作用。 What can be the reason? 可能是什么原因? Do I need to alter any settings on server? 我是否需要更改服务器上的任何设置?
Slim reads php://input
to get the contents of the request body, so whatever the problem is it has to do with the particulars of that stream. Slim读取php://input
以获取请求主体的内容,因此无论问题是什么,它都与该流的细节有关。
Do you have some other code that tries to read php://input
? 你有其他一些试图读取php://input
吗? If so, note that this is only possible starting from PHP 5.6 (which your local machine may have when your server does not). 如果是这样,请注意,这只能从PHP 5.6开始(当您的服务器没有时,您的本地计算机可能会有)。
尝试使用getInstance()。
$body = json_decode($app->getInstance()->request()->getBody());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.