Http
Request
Represents an HTTP request.
Method
Represents an HTTP method.
Header
Represents an HTTP header e.g. Content-Type: application/json
TimeoutConfig
Represents a timeout configuration for an HTTP request.
Response
Represents an HTTP response.
Err
Represents an HTTP error.
errorBodyToBytes
Convert the ErrorBody of a BadStatus error to List U8.
defaultRequest : Request
A default Request
value.
# GET "roc-lang.org" { Http.defaultRequest & url: "https://www.roc-lang.org", }
header : Str, Str -> Header
An HTTP header for configuring requests.
See common headers here.
handleStringResponse : Response -> Result Str Err
errorToString : Err -> Str
send : Request -> Task Response [HttpErr Err]
Task to send an HTTP request, succeeds with a value of Str
or fails with an
Err
.
# Prints out the HTML of the Roc-lang website. response = { Http.defaultRequest & url: "https://www.roc-lang.org" } |> Http.send! response.body |> Str.fromUtf8 |> Result.withDefault "Invalid UTF-8" |> Stdout.line
get : Str, fmt -> Task body [ HttpErr Http.Err, HttpDecodingFailed ] where body implements Decoding, fmt implements DecoderFormatting
Try to perform an HTTP get request and convert (decode) the received bytes into a Roc type. Very useful for working with Json.
import json.Json # On the server side we send `Encode.toBytes {foo: "Hello Json!"} Json.utf8` { foo } = Http.get! "http://localhost:8000" Json.utf8
getUtf8 : Str -> Task Str [HttpErr Http.Err]
methodToStr : Method -> Str
parseFormUrlEncoded : List U8 -> Result (Dict Str Str) [BadUtf8]
Parse URL-encoded form values (todo=foo&status=bar
) into a Dict (("todo", "foo"), ("status", "bar")
).
expect bytes = Str.toUtf8 "todo=foo&status=bar" parsed = parseFormUrlEncoded bytes |> Result.withDefault (Dict.empty {}) Dict.toList parsed == [("todo", "foo"), ("status", "bar")]
parseMultipartFormData : { headers : List Header, body : List U8 } -> Result (List MultipartFormData.FormData) [ InvalidMultipartFormData, ExpectedContentTypeHeader, InvalidContentTypeHeader ]
For HTML forms that include files or large amounts of text.
See usage in examples/file-upload-form.roc