diff --git a/platform/Http.roc b/platform/Http.roc index 4233c38f..581b9bc7 100644 --- a/platform/Http.roc +++ b/platform/Http.roc @@ -47,7 +47,7 @@ Err : InternalHttp.Error ## ``` ## # GET "roc-lang.org" ## { Http.defaultRequest & -## url: "https://www.roc-lang.org", +## url: "https://roc-lang.org", ## } ## ``` ## @@ -64,7 +64,6 @@ defaultRequest = { ## An HTTP header for configuring requests. ## ## See common headers [here](https://en.wikipedia.org/wiki/List_of_HTTP_header_fields). -## header : Str, Str -> Header header = Header @@ -94,15 +93,15 @@ errorToString = \err -> ## ## ``` ## # Prints out the HTML of the Roc-lang website. -## response <- -## { Http.defaultRequest & url: "https://www.roc-lang.org" } -## |> Http.send -## |> Task.await +## response = +## Http.send! { Http.defaultRequest & +## url: "https://www.roc-lang.org" +## } ## ## response.body ## |> Str.fromUtf8 ## |> Result.withDefault "Invalid UTF-8" -## |> Stdout.line +## |> Stdout.line! ## ``` send : Request -> Task Response [HttpErr Err] send = \req -> @@ -126,6 +125,25 @@ send = \req -> } |> Task.mapErr HttpErr +## Task to send an HTTP GET request and decode the response body. +## +## This function combines sending a GET request with decoding the response body. +## It returns a [Task] that succeeds with the decoded body or fails with either +## an [HttpErr] or [HttpDecodingFailed]. +## +## ``` +## # Get and decode a JSON response +## jsonResponse = +## Http.get! "https://api.example.com/data" Json.utf8 +## +## when jsonResponse is +## Ok data -> handleData data +## Err (HttpErr err) -> handleHttpError err +## Err HttpDecodingFailed -> handleDecodingError +## ``` +## +## @param url - The URL to send the GET request to +## @param fmt - The decoder format to use for decoding the response body get : Str, fmt -> Task body [HttpErr Http.Err, HttpDecodingFailed] where body implements Decoding, fmt implements DecoderFormatting get = \url, fmt -> response = send! { defaultRequest & url }