Reputation: 1238
I'm using Laravel 5.1 and I'm unable to use its Request injection.
If I print_r($request->all())
, I get an empty array:
Array
(
[\] =>
)
But when I check Request::getContent()
, it shows that I have content.
{"test": "test"}
Why is this? I have never had this problem before.
public function state(Requests\CheckState $request) {
print_r($request->all());
print_r($request->getContent());
}
class CheckState extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
];
}
}
Upvotes: 1
Views: 1295
Reputation: 2970
When sending raw JSON data to Laravel, be sure to specify Content-Type: application/json
This is because the Request
class checks for JSON content this way:
/**
* Determine if the request is sending JSON.
*
* @return bool
*/
public function isJson()
{
return Str::contains($this->header('CONTENT_TYPE'), '/json');
}
If the header is omitted, the framework assumes the request is plaintext.
Upvotes: 1