两个月前,我在laravel中创建了一个api并用邮递员进行了测试。一切都很好。 现在我会继续发展,但我不能像以前那样访问这些元素。
邮差:
体:
{
"RFQ" : "123",
"client_id": "2",
"ITEMS": [
{
"material" : "1.234.565",
"description" : "Test material 1",
"quantity" : "2.123",
"Quot. Deadline" : "2018-01-12",
"delivery_date" : "2018-01-12",
},
{
"material" : "9.87564.2",
"description" : "Test material 2",
"quantity" : "4",
"Quot. Deadline" : "2018-01-12",
"delivery_date" : "15.01.2018"
}
]
}
控制器:
public function import(ImportRequestForQuoteRequest $request, $id)
{
return $request->getContent();
}
之前,我能够以$request->client_id
之类的方式获取client_id,但现在却没有返回任何内容。
如果我返回$request->getContent()
,我会得到一个像身体一样的字符串。
访问这些值我需要做什么?
答案 0 :(得分:1)
尝试像这样返回
public function import(ImportRequestForQuoteRequest $request, $id)
{
return response()->json($request->getContent());
}
答案 1 :(得分:0)
你可以在控制器中试试这个......
use Illuminate\Http\Request;
public function myFunction(Request $request, $id)
{
$post_param = $request->post_param;
$default = '';
$post_param = $request->input('client_id', $default);
$route_param = $id;
return response()->json(['params' => $request->all()]);
}
答案 2 :(得分:0)
你需要将json解码为php数组,然后你才能像php中的普通数组一样访问
$item = json_decode($request->ITEMS);
答案 3 :(得分:0)
你的身体:
{
"RFQ" : "123",
"client_id": "2",
"ITEMS": [
{
"material" : "1.234.565",
"description" : "Test material 1",
"quantity" : "2.123",
"Quot. Deadline" : "2018-01-12",
"delivery_date" : "2018-01-12",
},
{
"material" : "9.87564.2",
"description" : "Test material 2",
"quantity" : "4",
"Quot. Deadline" : "2018-01-12",
"delivery_date" : "15.01.2018"
}
]
}
更改为:
{
"RFQ" : "123",
"client_id": "2",
"ITEMS": [
{
"material" : "1.234.565",
"description" : "Test material 1",
"quantity" : "2.123",
"Quot. Deadline" : "2018-01-12",
"delivery_date" : "2018-01-12"
},
{
"material" : "9.87564.2",
"description" : "Test material 2",
"quantity" : "4",
"Quot. Deadline" : "2018-01-12",
"delivery_date" : "15.01.2018"
}
]
}
json数组应该是格式良好的。因此,尝试从第一项的“delivery_date”中删除数组中的逗号。 并且您将使用$ request-> all()。
获得结果希望这能解决。
答案 4 :(得分:0)
我使用php storm REST Client做了同样的请求。使用相同的主体和标题,一切正常。
邮差在内容之前和之后添加"""
我不知道为什么。
所以错误在Postman的任何地方