Request and response

Swoft's request and response is implemented in PSR 7

The request and response objects exist in every HTTP request. The Request referred to here is Swoft\Http\Message\Request and the Response is Swoft\Http\Message\Response .

Note! According to the immutable of the PSR-7 object, all the with* methods are cloned and returned, and must receive new objects for further processing, or use chained calls.

basic method

The PSR-7 interface provides these public methods for request and response objects:

  • withProtocolVersion($version)
  • withHeader($name, $value)
  • withAddedHeader($name, $value)
  • withoutHeader($name)
  • withBody(StreamInterface $body)

The PSR-7 interface provides these methods for request objects:

  • withMethod(string $method)
  • withUri(UriInterface $uri, $preserveHost = false)
  • withCookieParams(array $cookies)
  • withQueryParams(array $query)
  • withUploadedFiles(array $uploadedFiles)
  • withParsedBody($data)
  • withAttribute($name, $value)
  • withoutAttribute($name)

The PSR-7 interface provides these methods for response objects:

  • withStatus($code, $reasonPhrase = '')

See PSR-7 for more details and see the specific implementation classes in swoft/http-message

Request object

How to get

  • Inject through the controller Action parameter
  • Get Swoft\Context\Context::mustGet()->getRequest() by request context

Request action method

 $request = \Swoft\Context\Context::mustGet()->getRequest();
$method = $request->getMethod(); 

Requested URI

Each HTTP request has a URI that identifies the requested application resource. The HTTP request URI has several parts:

  • Scheme (eg http or https )
  • Host (eg )
  • Port (eg 80 or 443 )
  • Path (eg /users/1 )
  • Query string (eg sort=created&dir=asc )

You can get the PSR-7 URI object by requesting the object's getUri() method:

 $uri = $request->getUri(); 

The URI of the PSR-7 request object is itself an object that provides the following methods to check the URL portion of the HTTP request:

  • getScheme()
  • getAuthority()
  • getUserInfo()
  • getHost()
  • getPort()
  • getPath()
  • getQuery() (eg a=1&b=2 )
  • getFragment()

Request Headers

All Headers

 $headers = $request->getHeaders();

foreach ($headers as $name => $values) {
    echo $name . ": " . implode(", ", $values).PHP_EOL;

Specified Header

  • The return value is array
 $headerValueArray = $request->getHeader('host');
  • The return value is a string
 $host = $request->getHeaderLine("host");

Some auxiliary methods

  • XHR
 if ($request->isAjax()) {
    // Do something
if ($request->isXmlHttpRequest()) {
    // Do something
  • GET
 if ($request->isGet()) {
    // Do something
  • POST
 if ($request->isPost()) {
    // Do something
  • PUT
 if ($request->isPut()) {
    // Do something
 if ($request->isDelete()) {
    // Do something
 if ($request->isPatch()) {
    // Do something
  • Content-Type
 $contentType = $request->getContentType(); 

Request data acquisition

GET data

 $data = $request->query();
$some = $request->query('key', 'default value')
$data = $request->get();
$some = $request->get('key','default value'); 

POST data

 $data = $request->post();
$some = $request->post('key', 'default value') 

GET & POST data

 $data = $request->input();
$some = $request->input('key', 'default value') 

JSON data

Only valid when Content-Type is application/json

 $data = $request->json();
$some = $request->json('key', 'default value') 

RAW data

 $data = $request->raw(); 


 $data = $request->getServerParams();
$some = $request->server('key', 'default value') 

Response object

How to get

  • Inject through the controller Action parameter
  • Get Swoft\Context\Context::mustGet()->getResponse() by request context Swoft\Context\Context::mustGet()->getResponse()

Common method

Output status code

 $response = \Swoft\Context\Context::mustGet()->Response();
return $response->withStatus(404); 

Output string content

 return $response->withContent("Hello Swoft2.0"); 

Output array

 $data = ['name'=>'Swoft2.0'];

Output header information

 return $response->withHeader("name","Swoft2.0"); 


 return $response->redirect("",302); 

file download

 return $response->file(\alias('@runtime/'),"application/octet-stream");