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.
spago install fetchNote:
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 runNode.js >=18.0 you don't need to set the --experimental-fetch node option.
Perform a simple GET request:
fetch "https://httpbin.org/get" {} >>= _.textPerform a POST request:
do
{ status, text } <- fetch "https://httpbin.org/post"
{ method: POST
, body: """{"hello":"world"}"""
, headers: { "Content-Type": "application/json" }
}
responseText <- textfetch works well with yoga-json and argonaut, use our little helper libraries.
spago install fetch-yoga-jsontype 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 worldspago install fetch-argonauttype 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