When using Cohttp_async
to perform a request, I'm handling an HTTP response code of 302 (Temporary Redirect) in the following manner:
let rec download uri =
Cohttp_async.Client.get uri
>>= fun (response, body) ->
let http_code = Cohttp.Code.code_of_status (Cohttp.Response.status response) in
if Cohttp.Code.is_redirection http_code then
(* Code to handle the redirect *)
else Cohttp_async.Body.to_string body
This seems to work alright (at least in the simple cases I'm using it). I'm mainly curious to see if there's a better way of doing this. I think there might be a better way to handle this, like by matching on Cohttp.Code.status
. Something like:
match http_code with
| Ok -> Cohttp_async.Body.to_string body
| Temporary_redirect -> (* Code to handle the redirect *)
| _ -> (* Failure here, possibly *)
So far, I haven't had much luck with this, as it seems like I'm not matching the right constructors.
As a second side question, does Cohttp have a better way to handle with HTTP redirects are given back as part of the response? Maybe the way I'm going about this is wrong, and there's a simpler way.