miso-1.12.0.0: A tasty Haskell front-end web framework
Copyright(C) 2016-2026 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <code@dmj.io>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

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 Response x -> action. The resulting Effect dispatches the appropriate action into the MVU loop when the response arrives.

Functions are grouped by HTTP method and body/response type:

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

JSON

getJSON Source #

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)

postJSON Source #

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.

postJSON' Source #

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.

putJSON Source #

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

getText Source #

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.

postText Source #

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.

putText Source #

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

getBlob Source #

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.

postBlob Source #

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.

putBlob Source #

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

getFormData Source #

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.

postFormData Source #

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.

putFormData Source #

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

getUint8Array Source #

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.

postUint8Array Source #

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.

putUint8Array Source #

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

postImage Source #

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.

putImage Source #

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

getArrayBuffer Source #

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.

postArrayBuffer Source #

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.

putArrayBuffer Source #

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 Body = JSVal Source #

Type synonym for a raw JavaScript request body.

data Response body Source #

Type returned from a fetch request

Constructors

Response 

Fields

Instances

Instances details
Functor Response Source # 
Instance details

Defined in Miso.FFI.Internal

Methods

fmap :: (a -> b) -> Response a -> Response b #

(<$) :: a -> Response b -> Response a #

FromJSVal body => FromJSVal (Response body) Source # 
Instance details

Defined in Miso.FFI.Internal

data CONTENT_TYPE Source #

List of possible content types that are available for use with the fetch API

Instances

Instances details
Show CONTENT_TYPE Source # 
Instance details

Defined in Miso.FFI.Internal

Eq CONTENT_TYPE Source # 
Instance details

Defined in Miso.FFI.Internal

ToJSVal CONTENT_TYPE Source # 
Instance details

Defined in Miso.FFI.Internal

Internal

fetch Source #

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