| Copyright | (C) 2016-2026 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Miso.Fetch
Description
Interface to the browser's
Fetch API
for making HTTP requests inside Miso's Effect monad.
Each function accepts a URL, optional request headers, a success callback,
and an error callback of the form . The resulting
Response x -> actionEffect dispatches the appropriate action into the MVU loop when the
response arrives.
Functions are grouped by HTTP method and body/response type:
- JSON —
getJSON,postJSON,postJSON',putJSON - Text —
getText,postText,putText - Blob —
getBlob,postBlob,putBlob - FormData —
getFormData,postFormData,putFormData - Uint8Array —
getUint8Array,postUint8Array,putUint8Array - ArrayBuffer —
getArrayBuffer,postArrayBuffer,putArrayBuffer - Image —
postImage,putImage
Use getJSON or postJSON for typical REST calls; use postJSON' when
the server also returns a JSON response body.
For Servant-style typed client generation, see the miso README.
Synopsis
- getJSON :: (FromJSON body, FromJSVal error) => MisoString -> [(MisoString, MisoString)] -> (Response body -> action) -> (Response error -> action) -> Effect parent props model action
- postJSON :: (FromJSVal error, ToJSON body) => MisoString -> body -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent props model action
- postJSON' :: (FromJSVal error, ToJSON body, FromJSON return) => MisoString -> body -> [(MisoString, MisoString)] -> (Response return -> action) -> (Response error -> action) -> Effect parent props model action
- putJSON :: (FromJSVal error, ToJSON body) => MisoString -> body -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent props model action
- getText :: FromJSVal error => MisoString -> [(MisoString, MisoString)] -> (Response MisoString -> action) -> (Response error -> action) -> Effect parent props model action
- postText :: FromJSVal error => MisoString -> MisoString -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent props model action
- putText :: FromJSVal error => MisoString -> MisoString -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent props model action
- getBlob :: FromJSVal error => MisoString -> [(MisoString, MisoString)] -> (Response Blob -> action) -> (Response error -> action) -> Effect parent props model action
- postBlob :: FromJSVal error => MisoString -> Blob -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent props model action
- putBlob :: FromJSVal error => MisoString -> Blob -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent props model action
- getFormData :: FromJSVal error => MisoString -> [(MisoString, MisoString)] -> (Response FormData -> action) -> (Response error -> action) -> Effect parent props model action
- postFormData :: FromJSVal error => MisoString -> FormData -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent props model action
- putFormData :: FromJSVal error => MisoString -> FormData -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent props model action
- getUint8Array :: FromJSVal error => MisoString -> [(MisoString, MisoString)] -> (Response Uint8Array -> action) -> (Response error -> action) -> Effect parent props model action
- postUint8Array :: FromJSVal error => MisoString -> Uint8Array -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent props model action
- putUint8Array :: FromJSVal error => MisoString -> Uint8Array -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent props model action
- postImage :: FromJSVal error => MisoString -> Image -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent props model action
- putImage :: FromJSVal error => MisoString -> Image -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent props model action
- getArrayBuffer :: FromJSVal error => MisoString -> [(MisoString, MisoString)] -> (Response ArrayBuffer -> action) -> (Response error -> action) -> Effect parent props model action
- postArrayBuffer :: FromJSVal error => MisoString -> ArrayBuffer -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent props model action
- putArrayBuffer :: FromJSVal error => MisoString -> ArrayBuffer -> [(MisoString, MisoString)] -> (Response () -> action) -> (Response error -> action) -> Effect parent props model action
- accept :: MisoString
- contentType :: MisoString
- applicationJSON :: MisoString
- textPlain :: MisoString
- formData :: MisoString
- type Body = JSVal
- data Response body = Response {
- status :: Maybe Int
- headers :: Map MisoString MisoString
- errorMessage :: Maybe MisoString
- body :: body
- data CONTENT_TYPE
- fetch :: (FromJSVal success, FromJSVal error) => MisoString -> MisoString -> Maybe JSVal -> [(MisoString, MisoString)] -> (Response success -> IO ()) -> (Response error -> IO ()) -> CONTENT_TYPE -> IO ()
JSON
Arguments
| :: (FromJSON body, FromJSVal error) | |
| => MisoString | url |
| -> [(MisoString, MisoString)] | headers |
| -> (Response body -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Retrieve a JSON resource via GET.
data Action = FetchGitHub | SetGitHub GitHub | ErrorHandler MisoString deriving (Show, Eq) updateModel :: Action -> Effect Model Action updateModel = case FetchGitHub -> getJSON "https://api.github.com" [] SetGitHub ErrorHandler SetGitHub apiInfo -> info ?= apiInfo ErrorHandler msg -> io_ (consoleError msg)
Arguments
| :: (FromJSVal error, ToJSON body) | |
| => MisoString | url |
| -> body | Body |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response () -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Send a POST request with a JSON-encoded body; ignores the response body.
Sets Content-Type: application/json automatically. Use postJSON' when
you also need to parse a JSON response body.
Arguments
| :: (FromJSVal error, ToJSON body, FromJSON return) | |
| => MisoString | url |
| -> body | Body |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response return -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Send a POST request with a JSON-encoded body and parse a JSON response.
Sets both Content-Type: application/json and Accept: application/json
automatically. Use postJSON when the response body is not needed.
Arguments
| :: (FromJSVal error, ToJSON body) | |
| => MisoString | url |
| -> body | Body |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response () -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Send a PUT request with a JSON-encoded body; ignores the response body.
Sets Content-Type: application/json automatically.
Text
Arguments
| :: FromJSVal error | |
| => MisoString | url |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response MisoString -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Retrieve a plain-text resource via GET.
Sets Accept: text/plain automatically. The response body is delivered as
a MisoString.
Arguments
| :: FromJSVal error | |
| => MisoString | url |
| -> MisoString | Body |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response () -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Send a POST request with a plain-text body; ignores the response body.
Sets Content-Type: text/plain automatically.
Arguments
| :: FromJSVal error | |
| => MisoString | url |
| -> MisoString | Body |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response () -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Send a PUT request with a plain-text body; ignores the response body.
Sets Content-Type: text/plain automatically.
Blob
Arguments
| :: FromJSVal error | |
| => MisoString | url |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response Blob -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Retrieve a binary resource as a Blob via GET.
Sets Accept: application/octet-stream automatically.
Arguments
| :: FromJSVal error | |
| => MisoString | url |
| -> Blob | Body |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response () -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Send a POST request with a Blob body; ignores the response body.
Sets Content-Type: application/octet-stream automatically.
Arguments
| :: FromJSVal error | |
| => MisoString | url |
| -> Blob | Body |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response () -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Send a PUT request with a Blob body; ignores the response body.
Sets Content-Type: application/octet-stream automatically.
FormData
Arguments
| :: FromJSVal error | |
| => MisoString | url |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response FormData -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Retrieve a multipart resource as FormData via GET.
Sets Accept: multipart/form-data automatically.
Arguments
| :: FromJSVal error | |
| => MisoString | url |
| -> FormData | Body |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response () -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Send a POST request with a FormData body; ignores the response body.
Sets Content-Type: multipart/form-data automatically.
Arguments
| :: FromJSVal error | |
| => MisoString | url |
| -> FormData | Body |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response () -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Send a PUT request with a FormData body; ignores the response body.
Sets Content-Type: multipart/form-data automatically.
Uint8Array
Arguments
| :: FromJSVal error | |
| => MisoString | url |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response Uint8Array -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Retrieve a binary resource as a Uint8Array via GET.
Sets Accept: application/octet-stream automatically.
Arguments
| :: FromJSVal error | |
| => MisoString | url |
| -> Uint8Array | Body |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response () -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Send a POST request with a Uint8Array body; ignores the response body.
Sets Content-Type: application/octet-stream automatically.
Arguments
| :: FromJSVal error | |
| => MisoString | url |
| -> Uint8Array | Body |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response () -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Send a PUT request with a Uint8Array body; ignores the response body.
Sets Content-Type: application/octet-stream automatically.
Image
Arguments
| :: FromJSVal error | |
| => MisoString | url |
| -> Image | Body |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response () -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Send a POST request with an Image body; ignores the response body.
Arguments
| :: FromJSVal error | |
| => MisoString | url |
| -> Image | Body |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response () -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Send a PUT request with an Image body; ignores the response body.
ArrayBuffer
Arguments
| :: FromJSVal error | |
| => MisoString | url |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response ArrayBuffer -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Retrieve a binary resource as an ArrayBuffer via GET.
Sets Accept: application/octet-stream automatically.
Arguments
| :: FromJSVal error | |
| => MisoString | url |
| -> ArrayBuffer | Body |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response () -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Send a POST request with an ArrayBuffer body; ignores the response body.
Sets Content-Type: application/octet-stream automatically.
Arguments
| :: FromJSVal error | |
| => MisoString | url |
| -> ArrayBuffer | Body |
| -> [(MisoString, MisoString)] | headers_ |
| -> (Response () -> action) | successful callback |
| -> (Response error -> action) | errorful callback |
| -> Effect parent props model action |
Send a PUT request with an ArrayBuffer body; ignores the response body.
Sets Content-Type: application/octet-stream automatically.
Header helpers
accept :: MisoString Source #
HTTP header name "Accept".
Use with =: to build request headers, e.g. accept =: applicationJSON.
contentType :: MisoString Source #
HTTP header name "Content-Type".
Use with =: to build request headers, e.g. contentType =: textPlain.
applicationJSON :: MisoString Source #
MIME type "application/json".
textPlain :: MisoString Source #
MIME type "text/plain".
formData :: MisoString Source #
MIME type "multipart/form-data".
Types
Type returned from a fetch request
Constructors
| Response | |
Fields
| |
data CONTENT_TYPE Source #
List of possible content types that are available for use with the fetch API
Instances
| Show CONTENT_TYPE Source # | |
Defined in Miso.FFI.Internal Methods showsPrec :: Int -> CONTENT_TYPE -> ShowS # show :: CONTENT_TYPE -> String # showList :: [CONTENT_TYPE] -> ShowS # | |
| Eq CONTENT_TYPE Source # | |
Defined in Miso.FFI.Internal | |
| ToJSVal CONTENT_TYPE Source # | |
Defined in Miso.FFI.Internal | |
Internal
Arguments
| :: (FromJSVal success, FromJSVal error) | |
| => MisoString | url |
| -> MisoString | method |
| -> Maybe JSVal | body |
| -> [(MisoString, MisoString)] | headers |
| -> (Response success -> IO ()) | successful callback |
| -> (Response error -> IO ()) | errorful callback |
| -> CONTENT_TYPE | content type |
| -> IO () |
Retrieve JSON via Fetch API
Basic GET of JSON using Fetch API, will be expanded upon.
See https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API