Advertisement
Guest User

Untitled

a guest
Oct 4th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. fetch('http://blt.dev/session/token')
  2. .then(
  3. function(response) {
  4. if (response.status !== 200) {
  5. console.log('Looks like there was a problem. Status Code: ' +
  6. response.status);
  7. return;
  8. }
  9.  
  10. // Examine the text in the response
  11. response.text().then(function(data) {
  12. console.log(data);
  13. });
  14. }
  15. )
  16. .catch(function(err) {
  17. console.log('Fetch Error :-S', err);
  18. });
  19.  
  20. fetch('http://blt.dev/api/v1/token?_format=hal_json', {
  21. method: 'POST',
  22. credentials: 'include',
  23. headers: {
  24. 'Content-Type': 'application/hal+json',
  25. 'X-CSRF-Token': 'GVRpRLO_GkJbPRd00JEfvXP7ycSIP4UYR766zuGchRc',
  26. 'Authorization': 'json_auth'
  27. },
  28. body: JSON.stringify({ username: 'admin', password: 'admin' })
  29. })
  30. .then(function(response){
  31. if(!response.ok){
  32. throw Error(response.statusText);
  33. }
  34. return response;
  35. })
  36. .then(function(response){
  37. return response.json()
  38. })
  39. .then(response => {
  40. try {
  41. console.log(response.token)
  42. return response.token;
  43. } catch (e) {
  44. console.log(e);
  45. }
  46. })
  47. .catch(error => {
  48. console.log(error);
  49. });
  50.  
  51. Error: Forbidden
  52. at <anonymous>:13:15
  53. at <anonymous>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement