not really known
Go to file
2023-12-06 23:38:29 +01:00
.github/workflows Update checks 2023-08-07 19:38:16 -04:00
src Allow request cancellation 2023-12-06 23:35:54 +01:00
test Allow request cancellation 2023-12-06 23:35:54 +01:00
.gitignore Depend on js-fetch instead of fetch-core 2023-11-16 13:50:09 +01:00
bower.json Update bower.json 2023-12-06 23:38:21 +01:00
CHANGELOG.md Update CHANGELOG.md 2023-08-07 19:47:33 -04:00
LICENSE Add license 2022-08-20 20:17:40 +01:00
packages.dhall Allow request cancellation 2023-12-06 23:35:54 +01:00
README.md Clarify that Node.js v18.0.0 does not require --experimental-fetch 2022-09-07 21:10:55 -04:00
spago.dhall Allow request cancellation 2023-12-06 23:35:54 +01:00
test.dhall Allow request cancellation 2023-12-06 23:35:54 +01:00

purescript-fetch

High-level library for the WHATWG Fetch Living Standard.

purescript-fetch works on browser and Node.js. Running on Node.js requires version >17.5, see # Usage.

Installation

spago install fetch

Usage

Note: Node.js <17.5 is not supported. Node.js >=17.5 and <18.0 requires the --experimental-fetch node options:

NODE_OPTIONS=--experimental-fetch spago run

Node.js >=18.0 you don't need to set the --experimental-fetch node option.

Perform a simple GET request:

fetch "https://httpbin.org/get" {} >>= _.text

Perform a POST request:

do
    { status, text } <- fetch "https://httpbin.org/post"
        { method: POST
        , body: """{"hello":"world"}"""
        , headers: { "Content-Type": "application/json" }
        }
    responseText <- text

Json parsing

fetch works well with yoga-json and argonaut, use our little helper libraries.

yoga-json

spago install fetch-yoga-json
type HttpBinResponse = { json :: { hello :: String } }

main :: Effect Unit
main = launchAff_ do
  { json } <- fetch "https://httpbin.org/post"
    { method: POST
    , body: writeJSON { hello: "world" }
    , headers: { "Content-Type": "application/json" }
    }
  { json: { hello: world } } :: HttpBinResponse <- fromJSON json
  log world

argonaut

spago install fetch-argonaut
type HttpBinResponse = { json :: { hello :: String } }

do
  { json } <- fetch "https://httpbin.org/post"
    { method: POST
    , body: toJsonString { hello: "world" }
    , headers: { "Content-Type": "application/json" }
    }
  { json: { hello: world } } :: HttpBinResponse <- fromJson json
  log world