I'm trying to access a custom header from the Request in Laravel. The header name is "accessing_from". Listing all the headers in Laravel, gives me only the "standard ones", but the one that I've set isn't present in the list. Checking in the browser network tab I can see that the header gets sent. So I'm wondering how to access it from within Laravel.
I'm using Angular2 to make the request with the default http service.
The Laravel's $response->header() dump:
The web inspector's log:
Thanks to anyone!
Are you talking about get parameter or something? If so, use:
request()->accessing_from;
For header you should use:
request()->header('accessing_from');
The working solution for this was the answer (the last one) of daver here:
Laravel get request header
Have you tried simple php?
<?php
// Replace XXXXXX_XXXX with the name of the header you need in UPPERCASE
$headerStringValue = $_SERVER['HTTP_XXXXXX_XXXX'];
Full answer:
https://stackoverflow.com/a/541463/3548658
The docs says:
https://laravel.com/api/5.3/Illuminate/Http/Request.html#method_header
use Request;
Request::header('accessing_from');
getting a custom header in Laravel 5.8.
this should apply to earlier versions as well.
If using a header like X-Requested-With: XMLHttpRequest you may notice that it converts this to HTTP_X_REQUESTED_WITH.
This, in turn, is converted to lower case version for the header() method.
request()->header('x_requested_with');
I would suggest using Accessing-From: admin which will add the apache header HTTP_ACCESSING_FROM. And you will be able to access it via the header function like this...
request()->header('accessing_from');
Related
I am actually completely baffled that this is such a difficult task to accomplish and/or find any relevant information about. My guess is that it must be something SO simple, that no one has to ask about it (except for me! :-) ), so I am hoping that someone can easily point me in the right direction...
I need to set headers in my Requests - not in my Responses (I've got that part handled), and not for Ajax routes (I've got that part handled as well). How on Earth do I accomplish this on internal app routes in Laravel 5.1?
Essentially, I need to attach an 'Authorization' header to certain Requests. (i.e.
$request->headers->set('Authorization', 'my-authorization-token');
)This line of code does not work, however. No matter where I put it. It doesn't work from middleware. It doesn't work from routes.php. It doesn't work from my controllers... it just simply does not work period. (For the sake of clarity, '$request' is 'Illuminate\Http\Request').
What am I missing? Where/How can I set request headers before a request is sent? Please help! Thanks in advance.
Some of the answers here might give you an idea, you could adapt them for the request: Where can I set headers in laravel
This also looks relevant: Laravel 5 / Lumen Request Header?
The request is sent from the client to the server (i.e. your Laravel app). So you set the request headers on the client site using Javascript.
The Laravel documentation has an example of setting the X-CSRF-TOKEN header using jQuery.
$.ajaxSetup({
headers: {
'X-MY-HEADER': 'whateveryouwant
}
});
Using VueJS it would look like this
Vue.http.headers.common['X-MY-HEADER'] = 'whateveryouwant';
You need to create a new request object and then set the header like this:
// e.g., Inside controller method
$request = new \Illuminate\Http\Request();
$request->setMethod('POST'); // or whatever your request type is
$request->header('Authorization', 'my-authorization-token');
I am having troubles with my PHP application which uses Drive SDK. I am trying to update a file, but all the time I receive 500 Internal Error message when I try to update file's contents.
I am looking for some way to debug the application. What would be most helpful for me is possibility to view how the entire request along with all headers look like. Is there any way to check it, or are there any other options for debugging?
Thank you a lot for your time.
I still didn't find any option for debugging- However, I found how I can view the requests done by the API client.
Open google-api-php-client/io/Google_REST.php file and find static public function execute(Google_HttpRequest $req) function.
There you will find this line:
$httpRequest = Google_Client::$io->makeRequest($req);
Right under it put the following code: var_dump($httpRequest);
During every request the client will do, you will get dump of it's request.
This question is still relevant but the accepted answer is very old. If you wish to view the HTTP requests and server responses in version three, the file you need to edit is /vendor/google/apiclient/src/Google/Http/REST.php. Locate the doExecute function and add print_r($request->getUri()); to the first line. Add print_r($response->getBody()->read(1024)); to the line just before the function returns to see the response body.
I am trying to enable CORS for an API built in CakePHP so that all requests are accessible with the following in the AppController:
public function beforeFilter()
{
header("Access-Control-Allow-Origin: *");
}
Is this in the wrong place? As requests are still being blocked.
Update: It seems this does in fact work BUT because I am doing something like:
header('Content-Type: application/json');
echo json_encode(array('message'=>'Hello world!'));
In some of my methods it's acting as though it's overriding the header set the AppController so it's not appearing in the response for the JSON calls.
Any ideas?
Update 2: Returning JSON like below, fixes the problem:
$this->response->type('json');
$this->response->body(json_encode(array('message'=>'Hello world!')));
So apparently using header() in Cake breaks previous headers?
You can do this using the cake response object;
$this->response->header('Access-Control-Allow-Origin', '*');
More info on the response object;
http://book.cakephp.org/2.0/en/controllers/request-response.html#setting-headers
However, the beforeRender() callback seems a more logical location.
Another option is to add this header in your apache vhost or htaccess examples can be found in the htaccess file of Html5Boilerplate which is a very interesting thing to look at (well documented), because it contains a lot of optimisations that work nicely with cakephp as well;
https://github.com/h5bp/server-configs-apache/blob/master/dist/.htaccess
http://html5boilerplate.com/
Based on what I found out here: Sending correct JSON content type for CakePHP
The correct way to return JSON in CakePHP is like so:
$this->response->type('json');
$this->response->body(json_encode(array('message'=>'Hello world!')));
This is because the headers can be overridden and therefore the CORS doesn't work unless you do it the 'proper' way using the response object in Cake.
I have a lithium app set up that way, so when
return($data)
is used it either can be used in the lithium view.html.php as
echo $data
or if request header "accept" equals "json/javacript" it would return something like
{
data: { a:'b' }
}
automatically.
Unfortunately in the new app that I made as a test app that flow is not happening (and only HTML is always returned).
After doing a little research it seems like that it is supposed to be done automatically if I uncomment
require __DIR__ . '/bootstrap/media.php';
inside bootstrap.php But that didn't work, I still have HTML returned. I downloaded a recent version of the lithium framework(I downloaded it may be 1 or 2 months ago)
Anybody knows if automatic response with JSON requires some set up or not?
taken from http://dev.lithify.me/lithium/tickets/view/353
which is then taken from the lithium docs
To enable automatic content-type negotiation (i.e. determining the content type of the response based on the value of the HTTP Accept header), set the 'negotiate' flag to true. Otherwise, the response will only be based on the type parameter of the request object (defaulting to 'html' if no type is present in the Request parameters)
http://li3.me/docs/lithium/action/Controller::$_render
If you need more help on how to implement leave a comment.
It is also possible to set type to $this->request->accepts() when calling render().
return $this->render(array('type' => $this->request->accepts()));
I'm trying to replace RSS polling with PubSubHubbub on my site. I'm able to use the subscriber library that google offers to send the subscription request. From the code it looks like it sends a post request via cURL with the RSS URL and a callback URL.
So this is where I need some direction:
In order to complete the subscription request my callback URL has to receive a GET request and then echo back a value from the GET request along with a 200 response. How do I get the parameters from the GET request? Is the echo done again via cURL? If so what option should include the 200 response?
This very simple script should be a start:
echo $_GET["request_name"];
this will output the GET parameter request_name and (implicitly) send a 200.
It's also a good idea to explicitly declare a content type before echoing, to prevent the default content type (usually "text/html") from kicking in:
header("Content-type: text/plain");
Note that when echoing external data, you may need to sanitize the output first - if the for example the output format is HTML, you would want to do something like echo htmlspecialchars($_GET["request_name"]); to prevent Cross-Site Scripting.
There was recently a thread on the php-dev mailing list about this. The reason you can't access 'hub.challenge' in the $_GET superglobal is due to register_globals. Basically PHP cleans up any argument names before creating the superglobals. Any dots will be converted to underscores. It's looking to be 'fixed' in PHP 6, but not before due to BC issues.
Here's the thread about it.