How to use the axios.defaults function in axios

To help you get started, weโ€™ve selected a few axios examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github 60late / vue-nodejs-blog / src / fetch / api.js View on Github external
import axios from 'axios'
import qs from 'qs';
import store from '../store/'
import router from '../router/'
// axios ้…็ฝฎ
axios.defaults.timeout = 10000;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8;multipart/form-data;boundary=----WebKitFormBoundaryeOOd9EoQVo6B4Tgb';
//    /api่ฟ™ไธชไฝ็ฝฎๅœจconfig/index.js้‡Œ้ข่ฎพ็ฝฎไบ†ไปฃ็†๏ผŒๆŒ‡ๅ‘ localhost:8081/api่ฟ™ไธชๅŽๅฐๅœฐๅ€
axios.defaults.baseURL = 'http://localhost:8081/api';

// http request ๆ‹ฆๆˆชๅ™จ
axios.interceptors.request.use(
    config => {
		if (store.state.token) {  // ๅˆคๆ–ญๆ˜ฏๅฆๅญ˜ๅœจtoken๏ผŒๅฆ‚ๆžœๅญ˜ๅœจ็š„่ฏ๏ผŒๅˆ™ๆฏไธชhttp header้ƒฝๅŠ ไธŠtoken
			config.headers.Authorization = `Bearer ${store.state.token}`;
		}
		console.log(config);
        return config;
    },
    err => {
        return Promise.reject(err);
    });
github nscozzaro / physics-is-beautiful / curricula / static / curricula / js / games / unit_conversion.jsx View on Github external
import React from 'react'
import PropTypes from 'prop-types'
import axios from 'axios'
// import RMathJax from 'react-mathjax' // alias as RMathJax instead global MathJax
import MediaQuery from 'react-responsive'
import {Prompt} from 'react-router-dom'
import Draggable from 'react-draggable'

import {ScoreBoard} from './score_board'
import {GameState} from '../constants'

var math = require('mathjs')
var Qty = require('js-quantities')

axios.defaults.xsrfHeaderName = 'X-CSRFToken'
axios.defaults.xsrfCookieName = 'csrftoken'

class UNITS {
  static get DISTANCE () {
    return {
      'mm': 'millimeters',
      'cm': 'centimeters',
      'km': 'kilometers',
      'ft': 'feet',
      'mi': 'miles'
    }
  }

  static get TIME () {
    return {
      'ms': 'milliseconds',
github crli / vue-sports / src / config / http.js View on Github external
import Vue from 'vue'
import router from '../router'
import axios from 'axios'
import { Indicator } from 'mint-ui';
import { Toast } from 'mint-ui';
axios.defaults.timeout = 30000;
axios.defaults.headers.common['Content-Type'] = 'application/json;charset=UTF-8'
// http request ๆ‹ฆๆˆชๅ™จ
axios.interceptors.request.use(
    config => {
      Indicator.open({
        text: 'ๅŠ ่ฝฝไธญ...',
        spinnerType: 'fading-circle'
      });
      return config;

    },
    err => {
      Indicator.close();
      Toast({
        message: 'ๅŠ ่ฝฝ่ถ…ๆ—ถ',
        position: 'middle',
        duration: 3000
github SandipNirmal / React-Native-MovieDB / src / services / index.js View on Github external
import * as axios from 'axios';

import Constant from './../utilities/constants';

axios.defaults.baseURL = Constant.api_base_url;;
axios.defaults.headers.post['Content-Type'] = 'application/json';

// Axios interceptor for handling common HTTP errors
// Need to use it with reducers
axios.interceptors.response.use(res => res, err => Promise.reject(error));

/**
 * HTTP request to search item in The MovieDB
 * 
 * @returns {object | Promise}
 */
const getConfiguration = () => {
  return axios.get(`/configuration?${Constant.api_key}`)
}

export { getConfiguration }
github alecortega / palettable / server / controllers / paletteController.js View on Github external
const axios = require('axios');
const _ = require('underscore');
const colornamer = require('color-namer');

axios.defaults.baseURL = 'http://www.colourlovers.com/api/palettes';
axios.defaults.params = { format: 'json' };

const _formatColors = colors => {
  return colors.map(color => `#${color}`);
};

const _flattenResponse = data => {
  return data.reduce((a, b) => a.concat(b.colors), data[0].colors);
};

const _removeShownColors = ({ newColors, dislikedColors, likedColors }) => {
  // Remove all colors that have been disliked or are already liked
  return newColors.filter(
    color =>
      dislikedColors.indexOf(`#${color}`) === -1 &&
      likedColors.indexOf(`#${color}`) === -1
github stripe / stripe-billing-typographic / client / src / auth.js View on Github external
clearToken() {
    // Remove the HTTP header that include the session token
    delete axios.defaults.headers.common['Authorization'];
    // Delete the token from our session
    sessionStorage.removeItem('token');
    this.token = null;
  },
  // Log out the user
github manosim / gitify / src / js / __tests__ / actions / index.js View on Github external
beforeEach(() => {
    axios.defaults.adapter = require('axios/lib/adapters/http');
  });
github openkfw / TruBudget / provisioning / src / index.js View on Github external
async function impersonate(userId, password) {
  const token = await authenticate(axios, userId, password);
  console.log(`Now logged in as ${userId}`);
  axios.defaults.headers.common.Authorization = `Bearer ${token}`;
}
github p2pu / learning-circles / frontend / components / learning_circle_form / RegistrationModal.jsx View on Github external
import React from 'react';
import axios from 'axios';

import Modal from 'react-responsive-modal';
import CheckboxWithLabel from 'p2pu-input-fields/dist/CheckboxWithLabel'
import InputWithLabel from 'p2pu-input-fields/dist/InputWithLabel'

import { API_ENDPOINTS } from '../../helpers/constants'

import '../stylesheets/learning-circle-form.scss';
import 'p2pu-input-fields/dist/build.css';


axios.defaults.xsrfCookieName = 'csrftoken'
axios.defaults.xsrfHeaderName = 'X-CSRFToken'

export default class RegistrationModal extends React.Component {

  constructor(props) {
    super(props);
    this.state = { user: this.props.user || { communication_opt_in: true }, errors: {}, registration: true };
    this.updateUserData = (data) => this._updateUserData(data);
    this.toggleModalType = () => this._toggleModalType();
    this.submitForm = (e) => this._submitForm(e);
  }

  _updateUserData(data) {
    this.setState({
      user: {
        ...this.state.user,
github wangmengHB / frontend-notes / 08 React ๅ’Œ Vue / vue-sample / src / api / config.js View on Github external
}
    return false;
}

let root;
if (isDevEnv()) {
    root = 'http://120.27.216.199:9080';
} else {
	root = `http://${location.host}`
}


/**
 *  ๆบๅธฆcookie
 **/
axios.defaults.withCredentials = true;

axios.interceptors.response.use(
	function (response) {
        indicator.hideBusy()
        if (response.data.code == 200) {
            return response;
        }

        if (response.data.code == 801 ||
            response.data.code == 802 ||
            response.data.code == 804
        ) {  
            router.push({name: USER_LOGIN})
            return Promise.reject(response.data.result);
        }