git.iko.soy/rowtype-yoga/purescript-fetch
on main help sign in
Add fetch-argonaut and simplify examples
commit9ac24ba1b635a5bd5b16c0f43cdf845f1515164b authorsigma-andex <77549848+sigma-andex@users.noreply.github.com> dateSat 20 Aug 2022, 20:13 (4 years ago) parent726b8d8
1 file changed+31−11
README.md+31−11
README.md+31−11
@@ -31,10 +31,8 @@ Perform a `POST` request:
3131do
3232 { status, text } <- fetch "https://httpbin.org/post"
3333 { method: POST
34− , mode: Cors
3534 , body: """{"hello":"world"}"""
3635 , headers: { "Content-Type": "application/json" }
37− , referrer: (ReferrerUrl "https://httpbin.org")
3836 }
3937 responseText <- text
4038```
@@ -44,18 +42,40 @@ do
4442`fetch` works well with `yoga-json` and `argonaut`, use our little helper libraries.
4543
4644### yoga-json
47−You can use `fetch-yoga-json` helper library to simplify json handling
45+
46+```bash
47+spago install fetch-yoga-json
48+```
4849
4950```purescript
50−do
51− { json } <- fetch "https://httpbin.org/post"
52− { method: POST
53− , body: writeJSON { hello: "world" }
54− , headers: { "Content-Type": "application/json" }
55− }
56− { "data": d, url, origin } <- fromJSON json
51+type HttpBinResponse = { json :: { hello :: String } }
52+
53+main :: Effect Unit
54+main = launchAff_ do
55+ { json } <- fetch "https://httpbin.org/post"
56+ { method: POST
57+ , body: """{"hello":"world"}"""
58+ , headers: { "Content-Type": "application/json" }
59+ }
60+ { json: { hello: world } } :: HttpBinResponse <- fromJSON json
61+ log world
5762```
5863
5964### argonaut
6065
61−Coming soon!
66+```bash
67+spago install fetch-argonaut
68+```
69+
70+```purescript
71+type HttpBinResponse = { json :: { hello :: String } }
72+
73+do
74+ { json } <- fetch "https://httpbin.org/post"
75+ { method: POST
76+ , body: """{"hello":"world"}"""
77+ , headers: { "Content-Type": "application/json" }
78+ }
79+ { json: { hello: world } } :: HttpBinResponse <- fromJson json
80+ log world
81+```