Reputation: 1221
I am implementing an API using Laravel 5.4 . I want to send the title, description, time and user_id as a JSON and after that to get the JSON response with the input data.
Here is my code:
$title = $request->input('title');
$description = $request->input('description');
$time = $request->input('time');
$user_id = $request->input('user_id');
$meeting = [
'title' => $title,
'description' => $description,
'time' => $time,
'user_id' => $user_id,
'view_meeting' => [
'href' => 'api/v1/meeting/1',
'method' => 'GET1'
]
];
$response = [
'msg' => 'Meeting created',
'meeting' => $meeting
];
return response()->json($response, 201);
After running the server, I make a post request using POSTMAN (body->raw:)
{
"time": "201601301330CET",
"title": "Test meeting 2",
"description": "Test",
"user_id": 2
}
But it return this:
{
"msg": "Meeting created",
"meeting": {
"title": null,
"description": null,
"time": null,
"user_id": null,
"view_meeting": {
"href": "api/v1/meeting/1",
"method": "GET1"
}
}
}
Why the title, description, time and user_id fields are null?
Upvotes: 2
Views: 1887
Reputation: 532
You need to set Postman's content-type
dropdown to JSON (application/json)
. Changing that setting changed the null
values in the response to:
{
"msg": "Meeting created",
"meeting": {
"title": "Test meeting 2",
"description": "Test",
"time": "201601301330CET",
"user_id": 2,
"view_meeting": {
"href": "api/v1/meeting/1",
"method": "GET1"
}
}
}
Upvotes: 3