简体   繁体   English

如何在Slim Rest API中访问POST请求的JSON请求正文?

[英]How to access a JSON request body of a POST request in Slim rest api?

i am trying to sent http post with json body and don't know how to handle with slim i tried diffrent soluctions found in stackoverflow but it showing error 500 i tryed 我正在尝试发送带有json正文的http帖子,并且不知道如何处理苗条,我尝试了在stackoverflow中找到的不同解决方案,但显示了我尝试的错误500
$app->post('/login', function () use ($app) { $json = $app->request->getBody(); $data = json_decode($json, true); });
but it returning "Slim Application Error A website error has occurred. Sorry for the temporary inconvenience." 但返回“ Slim Application Error发生网站错误。很抱歉给您带来暂时的不便。”

You only need to get the parsed request body from Slim 您只需要从Slim获取已解析的请求主体
Slim take care of decoding and sent it back to you as PHP array You also will add Request and Response in your route function Slim负责解码,并将其作为PHP数组发送给您。您还将在route函数中添加Request和Response
You code will be like that 您的代码将像这样

$app->post('/login', function ($request,$response){
    $Arr = $request->getParsedBody();
    //you did not need to re decode the body using this way     
    //$data = json_decode($json, true); 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM