Response object

Life cycle

The Response object exists in the system in singleton mode and is automatically created when the client HTTP request is received, until the request ends automatically. The Response object is fully compliant with all the specifications in [PSR7] ( Other details, interested students can view the corresponding code in the IDE.

Method list


This method is used to respond to data to the customer.

$this->response()->write('hello world');


This method is used to redirect the request to the specified URL



Set a cookie to the client in the same way as the native setCookie.


Used to get the original swoole_http_response instance.


End the response to the HTTP request. After the end, the data cannot be responded to the client again.


Determine whether the HTTP request ends the response. When you don't know whether the response has been ended, you can use this method to determine whether you can respond to the client again:

     $this->response()->write('Continue to send data');

PSR-7 Specification Common Methods in Response Objects


Send an HTTP status code to the client.


Note: $statusCode must be a standard HTTP allowed status code. See the Status object in the Http Message for details.


Used to send a header to an HTTP client.