httpaf icon indicating copy to clipboard operation
httpaf copied to clipboard

Example in README does not work out of the box

Open ukd1 opened this issue 3 years ago • 1 comments

Assuming a fresh install, and running:

dune init proj helloworld
opam install httpaf

add lib to the dune thing

(executable
 (public_name helloworld)
 (name main)
 (libraries helloworld httpaf))

the example in the readme does not compile........

open Httpaf
module String = Caml.String

let invalid_request reqd status body =
  (* Responses without an explicit length or transfer-encoding are
     close-delimited. *)
  let headers = Headers.of_list [ "Connection", "close" ] in
  Reqd.respond_with_string reqd (Response.create ~headers status) body
;;

let request_handler reqd =
  let { Request.meth; target; _ } = Reqd.request reqd in
  match meth with
  | `GET ->
    begin match String.split_on_char '/' target with
    | "" :: "hello" :: rest ->
      let who =
        match rest with
        | [] -> "world"
        | who :: _ -> who
      in
      let response_body = Printf.sprintf "Hello, %s!\n" who in
      (* Specify the length of the response. *)
      let headers =
        Headers.of_list
          [ "Content-length", string_of_int (String.length response_body) ]
      in
      Reqd.respond_with_string reqd (Response.create ~headers `OK) response_body
    | _ ->
      let response_body = Printf.sprintf "%S not found\n" target in
      invalid_request reqd `Not_found response_body
    end
  | meth ->
    let response_body =
      Printf.sprintf "%s is not an allowed method\n" (Method.to_string meth)
    in
    invalid_request reqd `Method_not_allowed response_body
;;
% dune build               
File "bin/main.ml", line 4, characters 16-27:
4 | module String = Caml.String
                    ^^^^^^^^^^^
Error: Unbound module Caml

ukd1 avatar Aug 08 '22 16:08 ukd1

I think that Caml is part of base specifically, so you're either meant to open Base or more simply elide the module String = Caml.String line. Not sure if something changed about the compiler at some point but I'm guessing the latter is the easier solution here.

dpatti avatar Aug 14 '22 20:08 dpatti