Home Get raw POST data in Laravel
Post
Cancel

Get raw POST data in Laravel

I recently stumbled upon an interesting problem when trying to retrieve the raw POST body in Laravel. This happened when I was sending POST/PUT requests from AngularJS to a REST API that was built with Laravel. I did a lot of trial and error before I figured out the problem.

To start of with we take a look at how we get the raw POST data in PHP. It’s a fairly simple command:

1
2
<?php
$rawPostData = file_get_contents("php://input");

The php://input is an I/O stream that you can read the raw POST data from. What is interesting though is that it can only be read once. And this is what caused me not being able to fetch it in this simple matter.

Why is that?

If you are familiar with Laravel you probably know that a lot of its core is built upon Symfony components, and the HTTP request handler is one of those components. If we dig in to the class Symfony\Component\HttpFoundation\Request we find this interesting line:

1
2
<?php
$this->content = file_get_contents('php://input');

Now you remember what I said about the I/O stream, right? I will repeat that, it can only be read once. Since the Symfony component reads the I/O stream, it’s empty after that. This results in that you can not access straight in your application with the simple approach I wrote before.

What then?

Lucky for us, the request instance is accessible through the Request facade in Laravel. The request instance then have the raw POST data set as its content.

1
2
3
4
5
6
<?php
// First we fetch the Request instance
$request = Request::instance();

// Now we can get the content from it
$content = $request->getContent();

Now we have the raw POST data in $content, simple as that.

This post is licensed under CC BY 4.0 by the author.
Contents

NPath complexity and cyclomatic complexity explained

Custom pagination in Laravel 4