aah Request and Response
This document provides an insights into aah Request and Response capabilities. So that you can use it effectively.
Request
aah Request object improved from standard request object. For typical application usage for e.g.: Parsing HTTP headers per RFC’s (Content-type, Accept content-type, Locale, Gzip), identifying Request Scheme, etc.
Fields
- Scheme - protocol value
httpsorhttp; it’s a derived value fromX-Forwarded-Protoif present used as-is,httpif TLS is nil andhttpsif TLS is not nil. - Host, Method, Path, Header - it is typically same as standard one, provided here for conventional access.
- ContentType - the parsed value of HTTP header
Content-TypeperRFC1521. - AcceptContentType - it is negotiated value from HTTP Header
Accept. In the order of URL extension, Accept header perRFC7231and Vendor Types perRFC4288 - AcceptEncoding - it is negotiated value from HTTP Header the
Accept-EncodingperRFC7231. - Locale - it is negotiated value from HTTP Header
Accept-LanguageperRFC7231. - Params - it contains values from Path, Form, Query and File.
- Payload - On v0.8 removed in-favor of Auto Parse and Bind or if you wanna access to request body use
Body().it holds the value from HTTP request body in bytes slice forContent-TypeJSON and XML otherwise nil. - ClientIP - remote client IP address aka Remote IP. Parsed in the order of
X-Forwarded-For,X-Real-IPand finallyhttp.Request.RemoteAddr. - IsGzipAccepted - whether client supported Gzip compression or not.
- Referer - value of HTTP header
RefererorReferrer. - UserAgent - value of HTTP header
User-Agent.
Methods
Learn about auto parse and bind here.
- PathValue - returns value for given Path param key otherwise empty string. For eg.:
/users/:userId=>PathValue("userId"). - QueryValue - returns value for given URL query param key otherwise empty string.
- QueryArrayValue - returns array value for given URL query param key otherwise empty string slice.
- FormValue - returns value for given form key otherwise empty string.
- FormArrayValue - returns array value for given form key otherwise empty string slice.
- FormFile - returns the first file for the provided form key otherwise returns error. It is caller responsibility to close the file.
- Body - Since v0.8 returns the request body as
io.Reader. - Cookie - returns a named cookie from HTTP request otherwise error.
- Cookies - returns all the cookies from HTTP request.
- IsJSONP - returns true if request URL query string has
callback=function_name. - IsAJAX - returns true if the request header
X-Requested-WithisXMLHttpRequestotherwise false. - Unwrap Since v0.7 - returns the standard Go HTTP request instance.
- SaveFile Since v0.8 - saves an uploaded multipart file for given key from the HTTP request into given destination file.
- SaveFiles Since v0.8 - saves an uploaded multipart file(s) for the given key from the HTTP request into given destination directory. It uses the filename as uploaded filename from the request.
Response
aah Response Writer is improved from standard response writer and compliant with http.ResponseWriter. So you can simply pass on ctx.Res anywhere it’s needed.
aah ahttp.ResponseWriter captures following details:
Status()- returns response status code.BytesWritten()- returns the total number of bytes written into response.Unwrap()- returns the standardResponseWriterif needed.
And Implements following standard interfaces from http library.
http.CloseNotifierhttp.Flusherhttp.Hijackerhttp.Pusherio.Closer
aah, the web framework for Go. Thank you!