0

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:

enter image description here

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_idbut 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?

1
  • what does dd($request->all()); return? Commented Mar 29, 2018 at 16:07

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

The example is only to test if i can access the values. I like to access the value of client_id to work with it in the controller.
My bad, i miss understood the quesiton @bmatovu answer says pretty much how you can access those parameters. if those don't work, you can always run them through json_encode
0

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()]);
}

4 Comments

If I return return $request->client_id; there is no output even return $request->getContent() shows the full content
Try dd($request->all()) to see what parameters are being passed to the function
dd($request->all()) return nothing
dd(request->getContent()) returns the body text: """ {\n "RFQ" : "123",\n "client_id": "2",\n "ITEMS": [\n {\n "material" : "1.234.565",\n "description" : "Test material 1",\n "quantity" : "2.123",\n "Quot. Deadline" : "2018-01-12",\n "delivery_date" : "2018-01-12",\n \n },\n {\n "material" : "9.87564.2",\n "description" : "Test material 2",\n "quantity" : "4",\n "Quot. Deadline" : "2018-01-12",\n "delivery_date" : "15.01.2018"\n \n }\n ]\n } """
0

you need to decode json to php array and then you able to access like normal array in php

$item = json_decode($request->ITEMS);

5 Comments

It returns null
so you first check data come from request or not through dd() function
No difference. It returns still null
becuse you json string is invalid check theme here and corre like:codebeautify.org/jsonviewer
i see that there are 3 times " in the string. but i have to search from where they are coming
0

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.

1 Comment

JSON is actually allowed to have trailing commas.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.