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 https or http; it’s a derived value from X-Forwarded-Proto if present used as-is, http if TLS is nil and https if 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-Type per RFC1521.
  • AcceptContentType - it is negotiated value from HTTP Header Accept. In the order of URL extension, Accept header per RFC7231 and Vendor Types per RFC4288
  • AcceptEncoding - it is negotiated value from HTTP Header the Accept-Encoding per RFC7231.
  • Locale - it is negotiated value from HTTP Header Accept-Language per RFC7231.
  • 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 for Content-Type JSON and XML otherwise nil.
  • ClientIP - remote client IP address aka Remote IP. Parsed in the order of X-Forwarded-For, X-Real-IP and finally http.Request.RemoteAddr.
  • IsGzipAccepted - whether client supported Gzip compression or not.
  • Referer - value of HTTP header Referer or Referrer.
  • 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-With is XMLHttpRequest otherwise 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 standard ResponseWriter if needed.

And Implements following standard interfaces from http library.

  • http.CloseNotifier
  • http.Flusher
  • http.Hijacker
  • http.Pusher
  • io.Closer