Search code examples
reactjsapireduxresponse

How to send headers in api with body empty using axios in react js.?


I'm trying to send an api call with access token in the header and empty body i'm sending the exact way but for some reasons it keeps on giving me error 401 which is Unauthorized i can't find the bug in it and i'm unable to resolve it.

My API call

export const resendSmsCode = (userdata) => {
  return async (dispatch) => {
    dispatch(fullScreenLoader(true));
    const response = await axios.post(
      "https://theappsouk.com/api/Profile/ReGenerateSmsCode",
      {
        Authorization: `Bearer ${userdata.auth}`,    
      }
    );
    const { data } = response;
    console.log(JSON.stringify(response))
    dispatch(fullScreenLoader(false));
    dispatch({
      type: RESEND_SMS_CODE,
      payload: data,
    });
  };
};

You can see in the picture its getting the api call


Solution

  • Looks like header options is supposed to be placed in the 3rd argument. But you placed it at 2nd argument which it sends your header as payload.

    Here is the correct one:

    const response = await axios.post(
      "https://theappsouk.com/api/Profile/ReGenerateSmsCode",
      void 0,
      {
        Authorization: `Bearer ${userdata.auth}`,    
      }
    );