Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get request header values in symfony

Tags:

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.

like image 986
Randhir Avatar asked Jul 03 '14 05:07

Randhir


2 Answers

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.

like image 114
Michael Sivolobov Avatar answered Oct 03 '22 08:10

Michael Sivolobov


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

like image 43
Mauricio Sánchez Avatar answered Oct 03 '22 09:10

Mauricio Sánchez