purescript-fetch/README.md
2022-08-19 16:46:55 +01:00

1.4 KiB

purescript-fetch

High-level library for the WHATWG Fetch Living Standard.

Note: This library requires Node.js 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
        , mode: Cors
        , body: """{"hello":"world"}"""
        , headers: { "Content-Type": "application/json" }
        , referrer: (ReferrerUrl "https://httpbin.org")
        }
    responseText <- text

Json parsing

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

yoga-json

You can use fetch-yoga-json helper library to simplify json handling

do
    { json } <- fetch "https://httpbin.org/post"
        { method: POST
        , body: writeJSON { hello: "world" }
        , headers: { "Content-Type": "application/json" }
        }
    { "data": d, url, origin } <- fromJSON json