Send HTTP requests.
Create a POST request. So imagine we want to send a POST request for
some JSON data. It might look like this:
import Json.Decode as JD
import Tepa exposing (Promise)
import Tepa.Http as Http
myProcedures : Promise m ()
myProcedures =
[ Debug.todo "some procedures"
, Tepa.bind
(Http.post
{ url = "https://elm-lang.org/assets/public-opinion.txt"
, requestBody = Http.EmptyRequestBody
}
)
<|
\result ->
case result of
GoodResponse meta rawBody ->
case JD.decodeString (JD.list JD.string) content of
Err err ->
[ proceduresForInvalidResponseBody err
]
Ok body ->
[ proceduresForJsonResponseBody body
]
_ ->
defaultHttpHandler result
]Notice that we are using decodeString to interpret the response
as JSON.
We did not put anything in the body of our request, but you can use constructors for the RequestBody if you need to send information to the server.
Create a custom request. For example, a PUT for files might look like this:
import File exposing (File)
import Tepa exposing (Promise)
import Tepa.Http as Http
type Msg
= Uploaded (Result Http.Error ())
upload :
File
->
Promise
c
m
(Result
Http.Error
( Http.Metadata, String )
)
upload file =
request
{ method = "PUT"
, headers = []
, url = "https://example.com/publish"
, requestBody = fileBody file
, timeout = Nothing
}It lets you set custom headers as needed. The timeout is the number of milliseconds you are willing to wait before giving up.
An HTTP header for configuring requests.
Create a Header.
header "If-Modified-Since" "Sat 29 Oct 1994 19:43:31 GMT"
header "Max-Forwards" "10"
header "X-Requested-With" "XMLHttpRequest"Represents the body of a request.
Create an empty body for your Request. This is useful for GET requests and POST requests where you are not sending any data.
Put some string in the body of your request. Defining jsonRequest looks like this:
import Json.Encode as JE exposing (Value)
jsonBody : Value -> Body
jsonBody value =
stringBody "application/json" (JE.encode 0 value)The first argument is a MIME type of the body. Some servers are strict about this!
Put some JSON value in the body of your request. This will automatically add the Content-Type: application/json header.
Use a file as the body of your request. When someone uploads an image into the browser with elm/file you can forward it to a server.
This will automatically set the Content-Type to the MIME type of the file.
Note: Use track to track upload progress.
Put some Bytes in the body of your request. This allows you to use elm/bytes to have full control over the binary representation of the data you are sending. For example, you could create an archive.zip file and send it along like this:
import Bytes exposing (Bytes)
zipBody : Bytes -> RequestBody
zipBody bytes =
bytesBody "application/zip" bytes
The first argument is a [MIME type](https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/MIME_types) of the body. In other scenarios you may want to use MIME types like `image/png` or `image/jpeg` instead.Note: Use track to track upload progress.
Extra information about the response:
url of the server that actually responded (so you can detect redirects)statusCode like 200 or 404statusText describing what the statusCode means a littleheaders like Content-Length and ExpiresNote: It is possible for a response to have the same header multiple times.
In that case, all the values end up in a single entry in the headers
dictionary. The values are separated by commas, following the rules outlined
here.
Create a
GETrequest.You can use functions like
expectStringexposed byelm/jsonto interpret the response as JSON value. In this example, we are expecting the response body to be aStringcontaining the full text of Public Opinion by Walter Lippmann.Note: Use
elm/urlto build reliable URLs.