2017-04-11 76 views
1

我实现使用Laravel 5.4的API。我想将标题,描述,时间和user_id作为JSON发送,然后用输入数据获取JSON响应。Laravel获得发送数据请求

这里是我的代码:

$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); 

运行的服务器后,我用邮差(体佩>原料:)

{ 
"time": "201601301330CET", 
"title": "Test meeting 2", 
"description": "Test", 
"user_id": 2 
} 

使POST请求,但它返回此:

{ 
"msg": "Meeting created", 
"meeting": { 
    "title": null, 
    "description": null, 
    "time": null, 
    "user_id": null, 
    "view_meeting": { 
    "href": "api/v1/meeting/1", 
    "method": "GET1" 
    } 
} 
} 

为什么标题,描述,时间和user_ID的领域都为空?

+1

转储$请求 - >所有(),看是否忠实地有附加到您的要求 – oseintow

+0

值是在头部的内容类型的问题邮递员 –

回答

2

您需要邮差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" 
    } 
    } 
} 
+0

非常感谢你!这解决了我的问题!你能否解释一下问题是什么以及内容类型有什么不同? –