-
Notifications
You must be signed in to change notification settings - Fork 53
Request and Response
Miloslav Hůla edited this page Jan 16, 2015
·
6 revisions
Classes Milo\Github\Http\Request and Milo\Github\Http\Response are envelopes for HTTP requests and response. They bring you a comfortable api to:
- add/set/get/check existency of HTTP headers
- get HTTP request method and URL
- get HTTP response code
- get HTTP request/response body
- make chain from responses
Class Milo\Github\Http\Request is the HTTP request envelope. Example of usage:
use Milo\Github\Http;
$method = Http\Request::POST;
$url = 'http://example.com/sniff';
$headers = [
'Content-Type' => 'application/json',
];
$content = json_encode($_SERVER);
$request = new Http\Request($method, $url, $headers, $content);
$request->getMethod(); # POST
$request->getUrl(); # http://example.com/sniff
$request->hasHeader('CONTENT-type'); # TRUE
$request->getHeader('Accept-Encoding', 'default/value'); # default/value
$request->getHeaders(); # ['content-type' => 'application/json']
$request->getContent(); # ...
$request->addHeader('Content-Type', 'text/html'); # does nothing
$request->setHeader('Content-Type', 'text/html'); # changes the header
Usually you don't need to create them. It is a job of HTTP clients.
Response is similar to request, almost all header methods are common. Example:
use Milo\Github\Http;
$code = 200;
$headers = [
'ETag' => '"fooooooooooooooooooooha"',
];
$content = json_encode($_SERVER);
$response = new Http\Response($code, $headers, $content);
$response->getCode(); # 200
$response->getContent(); # ...
$response->getPrevious(); # see below
HTTP clients perform a redirection on some HTTP response codes. You can get the previous response by Response::getPrevious()
. It is similar to exceptions chaining.
Second case of chaining is a response caching. CachedClient sets the 304 Not Modified
response as previous to response from cache.