Skip to content

Commit

Permalink
Fix form
Browse files Browse the repository at this point in the history
  • Loading branch information
Kruhlmann committed May 24, 2024
1 parent f82b643 commit 81a1c8b
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/form.ml
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
open Lwt.Syntax
open Cohttp_lwt
open Cohttp_lwt_unix

let post (url : Uri.t) (form_data : (string * string) list) =
let headers = Header.init_with "Content-Type" "application/x-www-form-urlencoded" in
let body = Uri.encoded_of_query form_data in
let* resp, body = Client.post ~headers ~body:(Body.of_string body) url in
let status = Response.status resp in
if Code.code_of_status status = 200
let headers = Cohttp.Header.init_with "Content-Type" "application/x-www-form-urlencoded" in
let body = Uri.encoded_of_query (List.map (fun (k, v) -> k, [ v ]) form_data) in
let* resp, body = Client.post ~headers ~body:(Cohttp_lwt.Body.of_string body) url in
let status = Cohttp.Response.status resp in
if Cohttp.Code.code_of_status status = 200
then
let* body = Body.to_string body in
let* body = Cohttp_lwt.Body.to_string body in
let json = Yojson.Basic.from_string body in
Lwt.return (Some json)
else (
let url_str = Uri.to_string url in
let* _ = Lwt_io.printl (Printf.sprintf "HTTP Error: %s for URL: %s" (Code.string_of_status status) url_str) in
let* _ =
Lwt_io.printl (Printf.sprintf "HTTP Error: %s for URL: %s" (Cohttp.Code.string_of_status status) url_str)
in
Lwt.return None)
;;

0 comments on commit 81a1c8b

Please sign in to comment.