ShinDarth ShinDarth - 13 days ago 6
PHP Question

Symfony unit test: how to set the request content type?

When creating a unit test with phpunit and Symfony (extending

WebTestCase
class), I need to perform a request specifying the content-type.

So I have my client:

$client->request('POST', '/my/route');


but the request content-type defaults to
application/x-www-form-urlencoded
, how can I change it?

E.g. setting it to
application/json

Answer

You have to add the content type in the fifth parameter:

$client->request('POST', '/my/route', [], [], ['CONTENT_TYPE' => 'application/json']);