webby

Web utilities - http headers and query parsing.


Need help? Read Nimble

Webby - Common HTTP data structures and functionality.

nimby install webby

Github Actions GitHub release (latest by date) GitHub Repo stars GitHub GitHub issues

API reference

About

Webby is a collection of common HTTP data structures and functionality. This includes things like Url, HttpHeaders and QueryParams.

This library has no dependencies other than the Nim standard library.

URL

  foo://admin:hunter1@example.com:8042/over/there?name=ferret#nose
  \_/   \___/ \_____/ \_________/ \__/\_________/ \_________/ \__/
   |      |       |       |        |       |          |         |
scheme username password hostname port   path       query   fragment

Use parseUrl to parse a URL:

let url = parseUrl("foo://admin:hunter1@example.com:8042/over/there?name=ferret#nose")
url.scheme == "foo"
url.username == "admin"
url.password == "hunter1"
url.hostname == "example.com"
url.port == "8042"
url.path == "/over/there"
url.query["name"] == "ferret"
url.fragment == "nose"

Note that the Url fields are stored in decoded form: /%6E%69%6D becomes /nim.

HTTP headers

Create a collection of HTTP headers:

var headers: HttpHeaders
headers["Content-Type"] = "image/png"

Check if a header is present:

if "Content-Encoding" in headers:
  echo headers["Content-Encoding"]

Iterate over the key-value pairs of headers:

for (k, v) in headers:
  echo k, ": ", v

Entries are stored in the order they are added. Procs like in, [] and []= are NOT case sensitive.

Query parameters

Parse a form-encoded string:

let
  search = "name=ferret&age=12&leg=1&leg=2&leg=3&leg=4"
  params = parseSearch(search)

Create a collection of query parameters:

var params: QueryParams
params["hash"] = "17c6d60"

Check if a parameter is present:

if "hash" in params:
  echo params["hash"]

Iterate over the query parameters:

for (k, v) in params:
  echo k, ": ", v

Entries are stored in the order they are added. Procs like in, [] and []= are case sensitive.

Repos using Webby

Some libraries using Webby include Mummy, Puppy and Curly.

Author: treeform

Latest versions: 0.1.9 0.2.0 0.2.1

Licence: MIT

Project website

Docs