Search code examples
reactjsreduxthunk

how to fix this axios call


I changed my code to handle API errors in one place and it stops working, can anyone identify what is the problem

before changing (working fine)

action.js

export const login = (email, password) => dispatch => {
  axios
    .post('http://localhost:8000/v1/users/signin/', {
      email: email,
      password: password,
    })
    .then(res => {
      dispatch({
        type: LOGIN_USER,
        payload: res.data,
      });
    })
    .catch(err => console.log(err));
};

after changing my code

action.js

import { postRequest } from '../services';

export const login = (email, password) => dispatch => {
  postRequest('users/signin/', {
    email: email,
    password: password,
  })
    .then(res => {
      dispatch({
        type: LOGIN_USER,
        payload: res.data,
      });
    })
    .catch(err => console.log(err));
};

services.js

export const API_URL = 'localhost:8000/v1/';

export const postRequest = (request, body) => {
   return axios.post(API_URL + request, body);
};

Solution

  • Did you forget 'http:' on API_URL?

    export const API_URL = 'http://localhost:8000/v1/';