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"
}
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.
Check that your request has:
Accept
with value application/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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With