Search code examples
datehttp-headersgleamellibirl

How do I format a date for an HTTP header in gleam?


I'm looking to set a Date header. It should be in this format:

Date: <day-name>, <day> <month> <year> <hour>:<minute>:<second> GMT
Date: Wed, 21 Oct 2015 07:28:00 GMT

Here's what I have so far:

import gleam/http/elli
import gleam/http/response.{type Response}
import gleam/http/request.{type Request}
import gleam/bytes_builder.{type BytesBuilder}
import birl

// Define a HTTP service
//
pub fn my_service(_request: Request(t)) -> Response(BytesBuilder) {
  let body = bytes_builder.from_string("Hello, world!")

  response.new(200)
  |> response.prepend_header("content-type", "text/plain")
  |> response.prepend_header("server", "gleam")
  |> response.prepend_header("date", birl.to_iso8601(birl.now()))
  |> response.set_body(body)
}

pub fn main() {
  elli.become(my_service, on_port: 8080)
}

Solution

  • It looks like birl has a to_http function.

    birl.to_http(birl.now())