Guest User

Untitled

a guest
Dec 13th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. Host: contenta.loc
  2. Connection: keep-alive
  3. Pragma: no-cache
  4. Cache-Control: no-cache
  5. Access-Control-Request-Method: GET
  6. Origin: http://localhost:3000
  7. User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36
  8. Access-Control-Request-Headers: authorization,x-csrf-token
  9. Accept: */*
  10. Accept-Encoding: gzip, deflate
  11. Accept-Language: en-US,en;q=0.9
  12.  
  13. import React, { Component } from 'react';
  14. import axios from 'axios';
  15. import Querystring from 'query-string';
  16. import SubRequests from 'd8-subrequests';
  17.  
  18. const URL = 'http://contenta.loc';
  19.  
  20. class App extends Component {
  21.  
  22. state = {
  23. csrfToken: '',
  24. oauthToken: {}
  25. };
  26.  
  27. constructor(props){
  28. super(props);
  29.  
  30. this.initializeCsrfToken();
  31. this.initializeOauthToken();
  32. }
  33.  
  34. initializeCsrfToken(){
  35. axios.get(URL + '/session/token')
  36. .then(response => {
  37. this.setState({csrfToken: response.data});
  38. })
  39. .catch((error) => {
  40. console.log('error ' + error);
  41. });
  42. }
  43.  
  44. initializeOauthToken(){
  45. const data = {
  46. grant_type: 'password',
  47. client_id: '77e40506-4b2a-4317-b6c0-5ed5b27ce886',
  48. client_secret: 'test1123',
  49. username: 'test',
  50. password: 'test'
  51. };
  52.  
  53. axios.post(URL + '/oauth/token', Querystring.stringify(data))
  54. .then(response => {
  55. this.setState({oauthToken: response.data.access_token});
  56. })
  57. .catch((error) => {
  58. console.log('error ' + error);
  59. });
  60. }
  61.  
  62.  
  63. fetchSubrequests() {
  64. const subrequests = new SubRequests(URL + '/subrequests?_format=json');
  65. const AuthStr = 'Bearer '.concat(this.state.oauthToken);
  66.  
  67. subrequests.add({
  68. uri: '/api/categories'
  69. });
  70.  
  71. subrequests.add({
  72. uri: '/api/tags'
  73. });
  74. subrequests.add({
  75. uri: '/api/menus'
  76. });
  77.  
  78. axios.get(subrequests.getUrl(), {
  79. headers: {
  80. Authorization: AuthStr,
  81. 'X-CSRF-Token': this.state.csrfToken,
  82. }
  83. }).then(dataresponse => {
  84. console.log(dataresponse);
  85. })
  86.  
  87. }
  88.  
  89. render = () => {
  90. return (
  91. <button >
  92. <div onClick ={
  93. () => {this.fetchSubrequests()}
  94. } > Fetch Subrequests</div>
  95. </button>
  96. )
  97. }
  98.  
  99. }
  100.  
  101. export default App;
Add Comment
Please, Sign In to add comment