代码之家  ›  专栏  ›  技术社区  ›  Hiren Gohel PRASANNA KUMAR K G

使用api调用时,Laravel如何解码HTTP请求正文内容类型:application/x-www-form-urlencoded

  •  2
  • Hiren Gohel PRASANNA KUMAR K G  · 技术社区  · 7 年前

    我正在使用Laravel-5.5,我正在为iOS应用程序开发一个API。

    在这里,我使用postman调用所有API。我有一个带有两个标题的注册api:

    (1) Authorization: Bearer access_token

    (2) Content-type: application/x-www-form-urlencoded

    现在,当我请求此api时,我只想打印 dd($request->all()); 但它没有给我正确的请求数据。我试过了 json_decode(urldecode(file_get_contents('php://input'))); 解码并尝试 $request->getContent(); 但运气不好!

    当使用时,是否有人知道如何在控制器中获取请求数据 内容类型:application/x-www-form-urlencoded ??

    编辑:

    public function registration(Request $request)
    {
         dd($request->all());
         dd($request->getContent());  //also tried but not work 
         dd(json_decode(urldecode(file_get_contents('php://input'))));   //no luck
    }
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   Hiren Gohel PRASANNA KUMAR K G    7 年前

    我是这样修复的:

    $requestData = json_decode($request->getContent(), true);
    dd($requestData);
    

    传递标头 Content-Type: application/json 在《邮差》中,它是固定的!!

    您只需执行以下操作:

    $requestData = json_decode($request->getContent(), true);
    

    您可以在您的请求中获得控制器中的所有请求数据!

    希望这将有助于任何用户在未来!!