Randhir
Randhir

Reputation: 765

How to get request header values in symfony

Am using symfony frame work in my application to make rest web service. I want to get request header values in controller method. Is there any way to achieve it.

Upvotes: 31

Views: 47140

Answers (3)

Mauricio Sánchez
Mauricio Sánchez

Reputation: 5282

If you need to get a specific header you can use:

$request->headers->get('My-Header');

See documentation: https://symfony.com/doc/current/components/http_foundation.html#accessing-accept-headers-data

Upvotes: 16

Michael Sivolobov
Michael Sivolobov

Reputation: 13240

You need to pass your Request object to the controller method and then in controller use $request->headers->all()

For example:

public function testAction(Request $request)
{
    $headers = $request->headers->all();
}

You can also get Request object from a controller by calling $this->getRequest() from controller method.

Upvotes: 49

kskaradzinski
kskaradzinski

Reputation: 5084

If You want header type $request->getMethod just like in forms this will git you POST|GET|PUT|DELETE request method

Upvotes: -1

Related Questions