Laravel 4 app (behind haproxy) Request::header() giving response headers? - php

When I do a GET request for a url on my laravel web app, I do the following in my controller to retrieve a custom header:
Request::header('customheader');
This value is always blank, though the header is clearly visible in Chrome Developer Tools in the Request Headers
While troubleshooting I tried using a standard header: Connection
Looking at the request headers, I expected the following:
Request::header('Connection') == "keep-alive"
What is super weird is that this instead returned "close" aka, the value of Connection in the Response Headers. This explains why my custom header is showing up empty (it is not in the response, but the request).
So what gives? The Laravel docs clearly state that this is the way to retreive REQUEST headers.
http://laravel.com/docs/4.2/requests#request-information
Any soltuions to get what I want?
edit: $_SERVER["HTTP_HEADER_NAME"] as proposed in https://stackoverflow.com/a/541450/1800023 gives me the same results. The values are those from the RESPONSE headers.

Related

Why do requests to my web API only succeed when Content-Type HTTP header is set?

I'm developing a REST-based web API with Yii2. Through testing, it seems that, in order to be successful, POST requests to the API need to have the Content-Type HTTP header set. (One tester was using Postman without setting the Content-Type header and was receiving 500 error responses in return, while another tester was using cURL and setting the Content-Type header and was receiving 200 success responses. When the Content-Type header is omitted, the POST data seems to be stripped from the request somewhere along the line, and no POST data gets logged by Yii for these requests.)
I was mostly following Yii's own guide for development and wasn't aware of any requirements in this area. Could someone explain why the Content-Type header must be set and what is happening otherwise?
I have discovered by looking in the Yii source code that there is a workaround.
When retrieving parameters that are submitted via POST (or other request methods) by calling the yii\web\Request::getBodyParam() method, the method checks if there is a parser set for the Content-Type of the request, but the method also checks if there is a parser set for Content-Type '*' (elseif (isset($this->parsers['*']))). So in my Yii application configuration, I just need to set the JsonParser for '*'...
'parsers' => [
'application/json' => 'yii\web\JsonParser',
'*' => 'yii\web\JsonParser'
]
and then I don't need to rely on clients having the Content-Type HTTP header set (as long as I handle the POST data sensibly).

php http post response for web hook

I'm trying to create a web hook notification. The documentation of the service i want to use requires that i specify a URL where POST requests can be performed. This URL will receive the following object, in json format, and must respond with a Status Code between 200-299.
{
"type": "ping"
}
I don't know how to proceed making my server on localhost respond with a 200 status code. http_response_code(200) works well on live server but nothing seem to be happening on localhost.
Is there any way i can make it work with localhost?
I've included the link to the documentation here (i hope it's not against the rule).
I am thinking that you wouldn't have to send them the response. The webhook would know about the response. If it reached your URL successfully, it would be a 200 OK right off the bat. If the API is requesting a response back then I imagine that you would have to call it back somehow. Is this a well-known API? Any documentation?
The response code is in the response header, not in the content.
PHP defaults to a response code of 200, so if you don't mess with it at all, you should be good.
If you want to set a different response code (202 for example), just call:
http_response_code(202);
Or set the full header yourself:
header('HTTP/1.1 202 Accepted');
Proper way to explicitly set 200 (or any other) status code with http_response_code function is just as following (don't echo or json_encode it):
http_response_code(200);
It should force webserver to use 200 status code in it's response. However, webserver could possibly ignore it. To check what response code your webserver sends, use telnet or any REST tool like Postman

Catch POST response from API with 302 status

I am trying to catch POST response send to me by external API.
The problem is that POST array is completely empty while I can check in firebug that browser recieved it but with codes 302 FOUND and second (with same body) with code 307 TEMPORARY REDIRECT:
Is there any way to grab this data inside my script or is this something wrong with server re-directions?
If you are using the CURL library, there are two options that help with your case:
curl_setopt($curl,CURLOPT_HEADER,1);
This returns the response header including the status code. You can see whether 302 is returned.
Or you can simply follow the redirect
curl_setopt($curl,CURLOPT_FOLLOWLOCATION,1);
Edit: sorry just saw you were doing this on the client side.
If this is an AJAX call, you can get the status code in the raw XHR object.

Cross-domain AJAX request error on HTTP 200

I'm writing a very basic Facebook app, but I'm encountering an issue with cross-domain AJAX requests (using jQuery).
I've written a proxy page to make requests to the graph via cURL that I'm calling via AJAX. I can visit the page in the browser and see it has the correct output, but requesting the page via always causes jQuery to fire the error handler callback.
So I have two files:
Proxy, which does the cURL request
<?php
//Do some cURL requests, manipulate some data
//return it as JSON
print json_encode($data);
?>
The facebook canvas, which contains this AJAX call
$.getJSON("http://myDomain.com/proxy.php?get=stuff",
function(JSON)
{
alert("success");
})
.error(function(err)
{
alert("err");
});
Inspecting the call with Firebug shows it returns with HTTP code 200 OK, but the error handler is always fired, and no content is returned. This happens whether I set Content-Type: application/json or not.
I have written JSON-returning APIs in PHP before using AJAX and never had this trouble.
What could be causing the request to always trigger the error handler?
Recently I experienced the same issue and my problem was the fact that there was a domain difference between the webpage and the API, due to the SSL.
The web page got a HTTP address (http://myDomain.com) and the content I was requesting with JQuery was on the same domain but HTTPS protocol (https://myDomain.com). The browser (Chrome in this case) considered that the domains were differents (the first one with HTTP, the second one with HTTPS), just because of the protocol, and because the request response type was "application/json", the browser did not allowed it.
Basically, the request worked fine, but your browser did not allowed the response content.
I had to add a "Access-Control-Allow-Origin" header to make it work. If you're in the same case, have a look there: https://developer.mozilla.org/en/http_access_control.
I hope that'll help you, I got a headache myself.

AJAX, Subdomains and the 200 OK response

A non-hypothetical but abstracted situation:
I have a domain www.foo.com, from which I'm making an AJAX POST to beta.foo.com. Examining the XHR object, I see a response header of 200 OK, but no response text - I even get a response 12B long, which is the exact response (a 12-character string) that I'm expecting - but the response text is blank.
If this is a cross-domain issue, why am I getting 200 OK, and better yet - why am I seeing the PHP functions fire on the beta.foo.com side - yet getting no response?
You can't do cross subdomains ajax calls that easy. There is something called Same origin policy that prevents you from doing that. If you want sort this issue you need to use JSONP or Iframes.
Install firebug and you will see an http 200 code and an error: that error is SOP acting.
You mentioned that you're checking the responseText property. Is it possible your response is in XML format?
If you send an XML request, or the response type is 'text/xml', you will get a value for responseXML. I believe that the responseText property can be blank if it's in XML format.
Random example from google:
http://javascript.about.com/library/blajax08.htm

Categories