-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
10 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
;; |