Guest User

Untitled

a guest
Apr 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. getHeadersWithToken(token) {
  2. return {
  3. 'Accept': 'application/json',
  4. 'Content-Type': 'application/json',
  5. 'dataType': 'json',
  6. 'Authorization': `Bearer ${token}`
  7. }
  8. }
  9.  
  10. fetchPost(url, params, otherToken) {
  11. var fetchHeaders = this.headers
  12. if (otherToken) {
  13. fetchHeaders = this.getHeadersWithToken(otherToken)
  14. }
  15.  
  16. var options = {
  17. headers: fetchHeaders,
  18. method: 'POST',
  19. body: JSON.stringify(params)
  20. }
  21.  
  22. return fetch(url, options)
  23. .then(resp => {
  24. var json = resp.json()
  25. if (resp.ok || (resp.status == 401) || (resp.status == 400)) {
  26. if (resp.statusText === 'Unauthorized') {
  27. json.then(err => { throw err })
  28. }
  29.  
  30. return json
  31. }
  32.  
  33. return json.then(err => { throw err })
  34.  
  35. })
  36. .catch(error => {
  37. throw error
  38. })
  39. }
Add Comment
Please, Sign In to add comment