Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Post Request empty Request parameters

When I post to my API endpoint with postman or my application the request parameter is an empty array.

With other controllers post requests work fine and the request param gets handled well. Only with my order controller does my post body not get processed.

I haven't made any changes and reverting to an older commit also did not help.

I'm at the point where I have no clue why it doesn't accept the JSON body.

Controller

public function createOrder(Request $request)
{
    return $request->product_id;
}

POST body | Raw JSON

{
    "product_id": 4
}

Response response is an empty array []"

I've tried to print the $request and I got this as response

POST /api/order/post HTTP/1.1
Accept:          application/json
Accept-Encoding: gzip, deflate, br
Authorization:   Bearer.{token}
Cache-Control:   no-cache
Connection:      keep-alive
Content-Length:  20
Content-Type:    
Cookie:          XSRF-TOKEN={Token}%3D%3D; laravel_session=%3D%3D
Host:            backend.host
User-Agent:      PostmanRuntime/7.22.0
Cookie: XSRF-TOKEN==={token}; laravel_session={Token}

{
    "product_id": 4
}

My post body is visible then so I'm confused why it's not showing

What I expect and what I get

I expect the request to return the value of my JSON body

what is happening nothing is returned

Note: I've recently added passport, but it was working fine last time I used this endpoint after adding laravel

Working controller

public function store(Request $request)
{
    return $request;
}

Api routes

Route::post('order/create', 'OrderController@createOrder');
Route::post('product/create', 'ProductController@store');

If I do echo $request in controller i see that there are data in the request, but I think that format is not good:

POST /api/messages HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, br
Authorization: Bearer 5|ibun75O2SNnZoT2W5Rk0KQox70wBxKN4xIIrq0sQ
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 522
Content-Type: application/json
Host: localhost:8000
User-Agent: PostmanRuntime/7.26.10

{
    "text" : "Molestiae dolor non eaque aut sapiente maiores. Aut voluptates nesciunt. Cum distinctio quisquam qui vitae eos
asperiores.

Nihil iste quia. Voluptate deleniti ipsam voluptas alias similique doloremque. Ratione alias iste voluptatem quis. Quis
quia quo sequi minus quod voluptate cum similique dolor.

Maxime enim aut. Eius ea doloribus. Quaerat dolor libero ipsum. Delectus atque esse ipsam est.",
    "imageLink" : "http://placeimg.com/640/480/abstract",
    "typeID" : "232",
    "answerTypeID" : "919"
}
like image 913
Salman Avatar asked Mar 03 '23 16:03

Salman


2 Answers

Add Content-Type:application/json to make PHP understand that it is receiving a JSON. Currently, your Content-type looks empty. Also make sure the CSRF token you pass is correct.

like image 96
nice_dev Avatar answered Mar 05 '23 15:03

nice_dev


Check that your request has:

  1. Add the header Accept with value application/json.
  2. If body is raw, make sure the type is JSON.
  3. If body is raw, make sure the content is a valid JSON and DO NOT include comments in JSON.

For example:

This works: raw body with type JSON:

{
    "email": "{{email}}",
    "password": "{{password}}"
}

This DOES NOT work: raw body even with type JSON:

{
    "email": "{{email}}", // Eg. [email protected] <-- This invalidates the JSON
    "password": "{{password}}" // Eg. password <-- This invalidates the JSON
}

Notice that in Postman, the JSON comments are visually/syntactically rendered as valid part of the JSON, but is actually invalid.

like image 42
doncadavona Avatar answered Mar 05 '23 16:03

doncadavona