Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- getHeadersWithToken(token) {
- return {
- 'Accept': 'application/json',
- 'Content-Type': 'application/json',
- 'dataType': 'json',
- 'Authorization': `Bearer ${token}`
- }
- }
- fetchPost(url, params, otherToken) {
- var fetchHeaders = this.headers
- if (otherToken) {
- fetchHeaders = this.getHeadersWithToken(otherToken)
- }
- var options = {
- headers: fetchHeaders,
- method: 'POST',
- body: JSON.stringify(params)
- }
- return fetch(url, options)
- .then(resp => {
- var json = resp.json()
- if (resp.ok || (resp.status == 401) || (resp.status == 400)) {
- if (resp.statusText === 'Unauthorized') {
- json.then(err => { throw err })
- }
- return json
- }
- return json.then(err => { throw err })
- })
- .catch(error => {
- throw error
- })
- }
Add Comment
Please, Sign In to add comment