我正在使用 Laravel 5.4 实现 API。我想以 JSON 形式发送标题、描述、时间和 user_id,然后获取包含输入数据的 JSON 响应。
这是我的代码:
$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);
运行服务器后,我使用 POSTMAN (body->raw:) 发出发布请求
{
"time": "201601301330CET",
"title": "Test meeting 2",
"description": "Test",
"user_id": 2
}
但它返回这个:
{
"msg": "Meeting created",
"meeting": {
"title": null,
"description": null,
"time": null,
"user_id": null,
"view_meeting": {
"href": "api/v1/meeting/1",
"method": "GET1"
}
}
}
为什么标题、描述、时间和 user_id 字段为空?
最佳答案
您需要将 Postman 的 content-type
下拉列表设置为 JSON (application/json)
。更改该设置将响应中的 null
值更改为:
{
"msg": "Meeting created",
"meeting": {
"title": "Test meeting 2",
"description": "Test",
"time": "201601301330CET",
"user_id": 2,
"view_meeting": {
"href": "api/v1/meeting/1",
"method": "GET1"
}
}
}
关于php - Laravel 获取发送到请求的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43353153/