HTTP::HEADER 1g 2024-03-17 laplante@plcb.ca GOWEB/HTTP


author: laplante@plcb.ca date: 2024-03-17 title: “HTTP::HEADER Function” version: 1.0.0 section: 1g

category: GOWEB/HTTP

Name

http::header — Retrieve all HTTP headers

Synopsis

http::header()

Description

The http::header function retrieves all HTTP headers from the incoming request and returns them as a map. Each header name is a key, and its value is represented as an array of strings (to handle multi-valued headers).

Examples

// Retrieve all headers
h := header();
"<h1>header</h1>";
stringify(indent: true, html: true, h);

// Might return:
header {
  "Accept": [
    "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
  ],
  "Accept-Encoding": [ "gzip, deflate, br, zstd" ],
  "Accept-Language": [ "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7" ],
  "Connection": [ "close" ],
  "Cookie": [ "_ga=GA1.1.654841373.1710437481; cd=abc" ],
  "Sec-Ch-Ua": [ "\"Chromium\";v=\"122\", \"Google Chrome\";v=\"122\"" ],
  "Sec-Ch-Ua-Mobile": [ "?0" ],
  "Sec-Ch-Ua-Platform": [ "\"macOS\"" ],
  "Sec-Fetch-Dest": [ "document" ],
  "Sec-Fetch-Mode": [ "navigate" ],
  "Sec-Fetch-Site": [ "none" ],
  "Sec-Fetch-User": [ "?1" ],
  "Upgrade-Insecure-Requests": [ "1" ],
  "User-Agent": [
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
  ]
}

Example: Parse the preferred language

package lang {
    func parse() {
        h := header();
        alstr := h["Accept-Language"];
        if alstr == "" {
            return [ "en" ];
        }
        newstr := replace_re(alstr[0], "", re/;q=\d+.\d+/);
        al := split(newstr, ",");
        return al;
    }
}

Author

laplante@plcb.ca

See also

Version