4

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"
}
4
  • Can you show your route, and also an example where this actually works? Commented Mar 15, 2020 at 14:11
  • @Christoffer Added it Commented Mar 15, 2020 at 14:14
  • Have you checked to see if there is (are) any middleware(s) in the way? Commented Mar 15, 2020 at 14:32
  • @UdoE. How do I check that? Commented Mar 15, 2020 at 14:36

2 Answers 2

9

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.

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

2 Comments

In addition to @vivek_23 's correct answer, I found in my case that the character encoding of my request was correct, but my actual body had characters which were invalid for the request. As Laravel was expecting different encoding to what was being received it was (I presume) converting the body to an empty string which was resulting in an empty request body. Essentially, sanitise (and if manually building; sense check) your actual body content!
In my case I had declared the let params = [] instead of let params = {} before pushing POST key=>value pairs into it, hence the empty $request->all()
5

Check that your request:

  1. Has 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.

1 Comment

The third topic saved my life and my job. Thank you

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.