Sending a JSON encoded entity to a RESTful API is a common need, and a JSON payload is actually pretty slick and convenient. It eases the pain of form-encoded strings when sending post data, and makes it easier to structure complex data that’s more readable. It can also make documentation easier to follow when you share cURL examples of making requests to your API. JSON is much easier for readers to digest compared to form encoded request params. So how do we do it in Lumen? I will show you a couple ways we can read JSON data and a middleware that can make it easy to support both Content-Type: application/json as well as the traditional Content-Type: application/x-www-form-urlencoded without polluting the controller with content-type specific logic. To illustrate, let’s say you are accepting a POST request to create a new blog post in your API and allowing JSON-encoded requests:

curl http://example.com/posts \
  -d '{"title": "My First Post!","body": "This is my first blog post!"}' \
  -H 'Content-Type: application/json'

To read the data in a Lumen route closure/controller, you can use the Illuminate\Http\Request::json() method like so:

<?php
$app->post('/posts', function (\Illuminate\Http\Request $request)
{
    dd(
        $request->json()->get('title'),
        $request->json()->all()
    );
});