Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Host: contenta.loc
- Connection: keep-alive
- Pragma: no-cache
- Cache-Control: no-cache
- Access-Control-Request-Method: GET
- Origin: http://localhost:3000
- 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
- Access-Control-Request-Headers: authorization,x-csrf-token
- Accept: */*
- Accept-Encoding: gzip, deflate
- Accept-Language: en-US,en;q=0.9
- import React, { Component } from 'react';
- import axios from 'axios';
- import Querystring from 'query-string';
- import SubRequests from 'd8-subrequests';
- const URL = 'http://contenta.loc';
- class App extends Component {
- state = {
- csrfToken: '',
- oauthToken: {}
- };
- constructor(props){
- super(props);
- this.initializeCsrfToken();
- this.initializeOauthToken();
- }
- initializeCsrfToken(){
- axios.get(URL + '/session/token')
- .then(response => {
- this.setState({csrfToken: response.data});
- })
- .catch((error) => {
- console.log('error ' + error);
- });
- }
- initializeOauthToken(){
- const data = {
- grant_type: 'password',
- client_id: '77e40506-4b2a-4317-b6c0-5ed5b27ce886',
- client_secret: 'test1123',
- username: 'test',
- password: 'test'
- };
- axios.post(URL + '/oauth/token', Querystring.stringify(data))
- .then(response => {
- this.setState({oauthToken: response.data.access_token});
- })
- .catch((error) => {
- console.log('error ' + error);
- });
- }
- fetchSubrequests() {
- const subrequests = new SubRequests(URL + '/subrequests?_format=json');
- const AuthStr = 'Bearer '.concat(this.state.oauthToken);
- subrequests.add({
- uri: '/api/categories'
- });
- subrequests.add({
- uri: '/api/tags'
- });
- subrequests.add({
- uri: '/api/menus'
- });
- axios.get(subrequests.getUrl(), {
- headers: {
- Authorization: AuthStr,
- 'X-CSRF-Token': this.state.csrfToken,
- }
- }).then(dataresponse => {
- console.log(dataresponse);
- })
- }
- render = () => {
- return (
- <button >
- <div onClick ={
- () => {this.fetchSubrequests()}
- } > Fetch Subrequests</div>
- </button>
- )
- }
- }
- export default App;
Add Comment
Please, Sign In to add comment