-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathfetch_examples.res
More file actions
68 lines (59 loc) · 1.75 KB
/
Copy pathfetch_examples.res
File metadata and controls
68 lines (59 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
open Webapi
open Promise
let _ = {
Fetch.fetch("/api/hellos/1")
->then(Fetch.Response.text)
->then(text => print_endline(text)->resolve)
}
let _ = {
Fetch.fetchWithInit("/api/hello", Fetch.RequestInit.make(~method="POST", ()))
->then(Fetch.Response.text)
->then(text => print_endline(text)->resolve)
}
let _ = {
Fetch.fetch("/api/fruit")
/* assume server returns `["apple", "banana", "pear", ...]` */
->then(Fetch.Response.json)
->then(json => Js.Json.decodeArray(json)->resolve)
->then(opt => Belt.Option.getExn(opt)->resolve)
->then(items =>
items->Js.Array2.map(item => item->Js.Json.decodeString->Belt.Option.getExn)->resolve
)
}
/* makes a post request with the following json payload { hello: "world" } */
let _ = {
let payload = Js.Dict.empty()
Js.Dict.set(payload, "hello", Js.Json.string("world"))
Fetch.fetchWithInit(
"/api/hello",
Fetch.RequestInit.make(
~method="POST",
~body=Fetch.BodyInit.make(Js.Json.stringify(Js.Json.object_(payload))),
~headers=Fetch.HeadersInit.make({"Content-Type": "application/json"}),
(),
),
)->then(Fetch.Response.json)
}
let _ = {
let formData = Webapi.FormData.make()
Webapi.FormData.appendObject(
formData,
"image0",
{"type": "image/jpg", "uri": "path/to/it", "name": "image0.jpg"},
(),
)
Fetch.fetchWithInit(
"/api/upload",
Fetch.RequestInit.make(
~method="POST",
~body=Fetch.BodyInit.makeWithFormData(formData),
~headers=Fetch.HeadersInit.make({"Accept": "*"}),
(),
),
)->then(Fetch.Response.json)
}
let _ = {
let controller = Fetch.AbortController.make()
let _ = Fetch.fetchWithInit("/api/fruit", Fetch.RequestInit.make(~signal=controller.signal, ()))
controller->Fetch.AbortController.abort
}