-
Notifications
You must be signed in to change notification settings - Fork 3
Response
chocs.HttpResponse
class is a part of request-response flow. Controllers must return instance of chocs.HttpResponse
as an output, only then output is recognised by chocs.Application
and used to generate real response to serve it to the client.
import chocs
response = chocs.HttpResponse()
import chocs
import json
response = chocs.HttpResponse("Response body", chocs.HttpStatus.OK)
json_response = chocs.HttpResponse(json.dumps({
"name": "John",
"last_name": "Doe",
}))
chocs.HttpStatus
is another convenience class to make the code easier to read and understand, int
values, like 200
are also accepted by initialiser but it is recommended to use chocs.HttpStatus
instead.
Learn more about chocs.HttpStatus
import chocs
from datetime import datetime
response = chocs.HttpResponse()
# simple cookie interface
response.cookies['simple-cookie'] = "Simple cookie for simple people"
# complex cookie
complex_cookie = chocs.HttpCookie("complex-cookie", "This cookie will expire in 2030-01-01", expires=datetime(2030, 1, 1))
response.cookies.append(complex_cookie)
Chocs
support dict-like interface in chocs.HttpResponse.cookie
attribute which is really convenient for simple cookie usage. When more control is needed it is recommended to rely on chocs.HttpCookie
class like in the example above.
import chocs
app = chocs.Application()
@app.get("/example")
def hello(request: chocs.HttpRequest) -> chocs.HttpResponse:
response = chocs.HttpResponse("Response body", chocs.HttpStatus.OK)
return response
chocs.serve(app)
chocs.HttpResponse.headers
: chocs.HttpHeaders
Contains dict like object which simplifies working with http headers.
chocs.HttpResponse.status_code
: chocs.HttpStatus
Status code returned back to the client. By default status code is 200 OK
.
chocs.HttpResponse.body
: typing.Union[str, bytes, io.BytesIO]
Data returned as a part of response object to the client. Supports multiple types by special setter defined inside the class, which casts all the supported types to io.BytesIO
instance. When accessed instance io.BytesIO
object is always returned.
chocs.HttpResponse.parsed_body
: typing.Union[str, bytes, io.BytesIO]
Read only attribute, returns body that is parsed to specified type. Depending on the HTTP response's content type header parsed body might be an instance of one of the following classes:
-
chocs.FormHttpMessage
when content type header is set toapplication/x-www-form-urlencoded
-
chocs.MultipartHttpMessage
when content type header is set tomultipart/form-data
-
chocs.JsonHttpMessage
when content type header is set toapplication/json
-
chocs.YamlHttpMessage
when content type header is set totext/yaml
,text/x-yaml
,application/x-yaml
chocs.HttpResponse.encoding
: str
Contains response's encoding, by default encoding is set to utf8
.
chocs.HttpRequest.cookies
: chocs.HttpCookieJar
Contains HTTP cookies stored as a dict like object.
chocs.HttpRequest.writable
: bool
Read only attribute. Checks if body is writable.
chocs.HttpRespose.write
(data
: typing.Union[str, bytes, bytearray]
) -> None
Writes contents into response's body.
chocs.HttpRespose.close
() -> None
Closes the stream and prevents body from being written.
chocs.HttpResponse.as_str()
-> str
Returns HTTP response's string representation.
chocs.HttpRespose.as_dict()
-> dict
Returns HTTP response's dict representation if any available
- Creating new application
- Registering a controller
- Grouping controllers
- Registering middleware
- Dynamically loading modules
TBA
TBA
- Reading request's body
- Accessing request's parsed body
- Accessing request's headers
- Accessing path's parameters
- Reading client cookies
- Comparing requests objects
- API