Post body is null when sending application/octet-stream via ajax

Hi there,

New to Slim in general, but I’m getting the hang of it. Anyway, I’m sending an ajax post to one of my routes, but the body is turning up null. I verified the output of the data being sent to the route via ajax and it’s working just fine.

Here’s the ajax request:

$.ajax({
	type: 'POST',
	url: 'http://localhost:8888/aslists/public/sign-in',
	contentType: 'application/octet-stream; charset=utf-8',
	data: authResult["code"], // sends a string to the server
	success: function(result) {
		console.log(result);
	},
	processData: false
});

For my route file I have:

 $app->post('/sign-in', '\Foo\Controllers\LoginController:login');

For the method inside of LoginController I have:

public function login($request, $response) {
	var_dump($request->getParsedBody());
}

I’m not really sure why this is failing. I played around with other content types like text/html and text/xml but I still got null

In order to decode the HTTP message into an array that is retrievable using getParsedBody , Slim uses the Content-Type header to determine how to do the decode. A Content-Type of `application/octet-stream’ is unknown to Slim and so it cannot decode the data.

The easiest solution is to send a Content-Type of application/json. Alternatively, you could register your own media type parser with the request as documented here: http://www.slimframework.com/docs/objects/request.html#media-type-parsers.

I was actually able to get it to behave with:

 $request->getBody()->getContents()

since I was just POSTing an access code from OAuth 2.0. For anyone struggling with this, you can split up your route file into a controllers and then get your access/refresh tokens like this:

 $client = new \Google_Client(/* optionally add config file here*/);
 $client->authenticate($request->getBody()->getContents());

Nevertheless, thank you @akrabat for the information!