Skip to content

Latest commit

 

History

History
63 lines (52 loc) · 1.86 KB

header.md

File metadata and controls

63 lines (52 loc) · 1.86 KB

Headers

back

Table of Contents

Request Header

To get request header, use request.headers

middleware/check_login.nim

import basolato/middleware

proc hasLoginId*(request:Request, params:Params):Future[Response] {.async.} =
  try:
    let loginId = request.headers["X-login-id"]
  except:
    raise newException(Error403, "Can't get login id")

app/controllers/sample_controller.nim

proc index*(request:Request, params:Params):Future[Response] {.async.} =
  let loginId = request.headers["X-login-id"]

Response header

Type of headers

newHttpHeaders() generate HttpHeaders object from openArray[tuple[key: string, val: string]].
https://nim-lang.org/docs/httpcore.html#HttpHeaders

let headers = {
  "Strict-Transport-Security": "max-age=63072000, includeSubdomains",
  "X-Frame-Options": "SAMEORIGIN",
  "X-XSS-Protection": "1, mode=block",
  "X-Content-Type-Options": "nosniff",
  "Referrer-Policy": "no-referrer, strict-origin-when-cross-origin",
  "Cache-control": "no-cache, no-store, must-revalidate",
  "Pragma": "no-cache",
}.newHttpHeaders()

Set headers in controller

Create header instance by newHttpHeaders() and add key and value. Finally, set header at the last arge of response.

proc index*(request:Request, params:Params):Future[Response] {.async.} =
  let headers = newHttpHeaders()
  headers.add("Controller-Header-Key1", ["Controller-Header-Val1", "Controller-Header-Val2"])
  headers.add("Controller-Header-Key2", ["val1", "val2", "val3"])
  return render("with header", headers)