Reputation: 771
Two month ago, i created an api in laravel and tested it with postman. Everything worked fine. Now i would continue to develop, but i can't access the elements like before.
Postman:
Body:
{
"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"
}
]
}
Controller:
public function import(ImportRequestForQuoteRequest $request, $id)
{
return $request->getContent();
}
Before, i was able to get as example the client_id like $request->client_id
but now it returns nothing.
If i return $request->getContent()
i get a string like the body.
What i have to do to access the values?
Upvotes: 0
Views: 405
Reputation: 771
I did the same request with the php storm REST Client. With the same body and headers, everything works fine.
Postman adds before and after the content """
I don't know why.
So the error is anywhere in Postman
Upvotes: 0
Reputation: 1786
Your Body:
{
"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"
}
]
}
Change with :
{
"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 array should be well-formed. So try to remove comma from array at "delivery_date" of first item. and you will get results using $request->all().
Hope this solves.
Upvotes: 0
Reputation: 2936
you need to decode json to php array and then you able to access like normal array in php
$item = json_decode($request->ITEMS);
Upvotes: 0
Reputation: 4074
You can try this in you controller...
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()]);
}
Upvotes: 0
Reputation: 2523
Try to return it like this
public function import(ImportRequestForQuoteRequest $request, $id)
{
return response()->json($request->getContent());
}
Source docs: https://laravel.com/docs/5.6/responses#json-responses
Upvotes: 1