How to adapt this axios object with bearer tokens that allows GET, to use POST methods?

Tags: ,



I have managed to make this run: How to modify axios instance after exported it in ReactJS?

And it looks like this:

    import axios from 'axios';
import constants from '../constants.js';
import Cookies from 'js-cookie';

const API = axios.create({
  baseURL: `${constants.urlBackend}`,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json',
  },
});

API.interceptors.request.use(
  config => {
    var accesstoken = Cookies.get('accesstoken');

    if (accesstoken) {
      config.headers.Authorization = `Bearer ${accesstoken}`;
    } else {
      delete API.defaults.headers.common.Authorization;
    }
    return config;
  },

  error => Promise.reject(error)
);

export default API;

And this is an example usage

getUserList() {
    API.get('/userlist')
        .then(response => {

            this.setState({
                userList: response.data
            }, () => {
                console.log(this.state.userList)
            });
        })
}

But now im confused because I dont understand how to use this with a post so I can pass some data to it, similar to this

axios({
            method: 'post',
            url: constants.urlBackend + "/register",
            data: qs.stringify({ email, password }),
            headers: {
                'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
            }
        })

But using the above object.

API.post('/user/update/'+this.state.rowId).then(response => {
        //some sort of body {email,password}
    })

Answer

Have you tried

API.post(
  '/user/update/' + this.state.rowId, {
    email,
    password
  }).then(response => {})


Source: stackoverflow