Hiren Gohel
Hiren Gohel

Reputation: 5042

How is Laravel decoding HTTP request body Content-Type: application/x-www-form-urlencoded when using api call

I'm using Laravel-5.5 and i'm developing an apis for an iOS app.

Here, i'm using postman to call all apis. I've one registration api with two headers:

1) Authorization: Bearer access_token

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

Now when i requesting this api i just want to print dd($request->all()); But it's not give me proper request data. I've tried json_decode(urldecode(file_get_contents('php://input'))); to decode it and also tried $request->getContent(); but no luck!

Can anyone knows how to get request data in controller when using Content-type: application/x-www-form-urlencoded??

EDIT:

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
}

Upvotes: 2

Views: 5910

Answers (1)

Hiren Gohel
Hiren Gohel

Reputation: 5042

I've fixed it like this:

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

Pass headers Content-Type: application/json in postman and it's fixed!!

You only need to do this:

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

and you can get all request data in your controller in your request!

Hope this will helps to any users in future!!

Upvotes: 3

Related Questions