Use the request object’s getBody() method to fetch the raw HTTP request body sent by the HTTP client. This is particularly useful for applications that consume JSON or XML requests.

Route::get('/hello/:name', function(){
  $body = $this->request->getBody();

How to Deal with Content-Types

By default Alter does not parse any other content-type other than the standard form data because PHP does not support it. That means if you attempt to post application/json you will not be able to access the data via $this->request->post();

To solve this you must parse the content type yourself. You can either do this on a per-route basis

//For application/json
$data = json_decode($this->request->getBody());


Get Cookies

Alter will automatically parse cookies sent with the current HTTP request. You can fetch cookie values with the request object like this:

$cookies = $this->request->cookies;

Only Cookies sent with the current HTTP request are accessible with this method. If you set a cookie during the current request, it will not be accessible with this method until the subsequent request.

You can optionally choose to encrypt all cookies stored on the HTTP client with the Slim app’s cookies.encrypt setting. When this setting is true, all cookies will be encrypted using your designated secret key and cipher.

It’s really that easy.


Alter will automatically parse all HTTP request headers. You can access the request headers using the request object’s headers property.

// Get request headers as associative array
$headers = $this->request->headers;

// Get the ACCEPT_CHARSET header
$charset = $this->request->headers->get('ACCEPT_CHARSET');

The HTTP specification states that HTTP header names may be uppercase, lowercase, or mixed-case. Slim is smart enough to parse and return header values whether you request a header value using upper, lower, or mixed case header name, with either underscores or dashes. So use the naming convention with which you are most comfortable.

Ajax Requests

When using a Javascript framework like MooTools or jQuery to execute an XMLHttpRequest, the XMLHttpRequest will usually be sent with a X-Requested-With HTTP header. The Slim application will detect the HTTP request’s X-Requested-With header and flag the request as such. If for some reason an XMLHttpRequest cannot be sent with the X-Requested-With HTTP header, you can force the Alter to assume an HTTP request is an XMLHttpRequest by setting a GET, POST, or PUT parameter in the HTTP request named “isajax” with a truthy value.

Use the request object’s isAjax() or isXhr() method to tell if the current request is an XHR/Ajax request:

$isXHR = $app->request->isAjax();
$isXHR = $app->request->isXhr();


The Request object provides several helper methods to fetch common HTTP request information:

Content Type

Fetch the request’s content type (e.g. “application/json;charset=utf-8”):


Media Type

Fetch the request’s media type (e.g. “application/json”):


Media Type Params

Fetch the request’s media type parameters (e.g. [charset => “utf-8”]):


Content Charset

Fetch the request’s content character set (e.g. “utf-8”):


Content Length

Fetch the request’s content length:



Fetch the request’s host (e.g. “”):


Host with Port

Fetch the request’s host with port (e.g. “”):



Fetch the request’s port (e.g. 80):



Fetch the request’s scheme (e.g. “http” or “https”):



Fetch the request’s path (root URI + resource URI):



Fetch the request’s URL (scheme + host [ + port if non-standard ]):


IP Address

Fetch the request’s IP address:



Fetch the request’s referrer:


User Agent

Fetch the request’s user agent string:


Request Methods

Every HTTP request has a method (e.g. GET or POST). You can obtain the current HTTP request method via the Request object:

 * What is the request method?
 * @return string (e.g. GET, POST, PUT, DELETE)

 * Is this a GET request?
 * @return bool

 * Is this a POST request?
 * @return bool

 * Is this a PUT request?
 * @return bool

 * Is this a DELETE request?
 * @return bool

 * Is this a HEAD request?
 * @return bool
 * @return bool

 * Is this a OPTIONS request?
 * @return bool

 * Is this a PATCH request?
 * @return bool

 * Is this a XHR/AJAX request?
 * @return bool

Leave a comment

Your email address will not be published. Required fields are marked *