Laravel post with content type json

- 1 answer

Ad

I'm working with laravel 5 framework, i want to create a rest API and i'm getting some problems. I have a method post that receives Json and i can't access that content, always say is [] or null

That's my controller function:

public function Register()
{            
    $teste = Request::json()->all();     
    $name=$teste['wtf'];
    return  $name;            
}

That returns me this error "Undefined index: wtf" because the array $teste is null

How can I receive JSON properly?

Ad

Answer

Ad

From the image you showed in the comment, I realize that the data you are sending is as form-data and not JSON. Please select raw option and choose json instead of text from drop down and retry what @Moppo said

Ad
source: stackoverflow.com
Ad