· 7 years ago · Dec 13, 2017, 11:24 PM
1Host: contenta.loc
2Connection: keep-alive
3Pragma: no-cache
4Cache-Control: no-cache
5Access-Control-Request-Method: GET
6Origin: http://localhost:3000
7User-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
8Access-Control-Request-Headers: authorization,x-csrf-token
9Accept: */*
10Accept-Encoding: gzip, deflate
11Accept-Language: en-US,en;q=0.9
12
13import React, { Component } from 'react';
14import axios from 'axios';
15import Querystring from 'query-string';
16import SubRequests from 'd8-subrequests';
17
18const URL = 'http://contenta.loc';
19
20class 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
101export default App;