httpService.js
1.52 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
import {stringify} from 'query-string';
module.exports = {
postForm: (targetAPIUri, formData) => {
return fetch(targetAPIUri, {
method: 'POST',
credentials: "include",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: formData,
}).then((res) => {
let res2 = res.json().then((response) => {
return response
}).catch((err) => {
console.log("Err: ", res)
});
return res2;
})
},
postJson: (targetAPIUri, formData) => {
return fetch(targetAPIUri, {
method: 'POST',
credentials: "include",
headers: {'Accept': 'application/json', 'Content-Type': 'application/json'},
body: JSON.stringify(formData),
}).then((res) => {
let res2 = res.json().then((response) => {
return response
}).catch((err) => {
console.log("Err: ", res)
});
return res2;
})
},
get: (targetAPIUri, query) => {
if (query) {
targetAPIUri = `${targetAPIUri}?${stringify(query)}`
}
return fetch(targetAPIUri, {
method: 'get',
credentials: "include",
}).then((res) => {
let res2 = res.json().then((response) => {
return response
}).catch((err) => {
console.log("Err: ", res)
});
return res2;
})
}
}