Home > Mobile >  Why my Authorization value not passed through Request Headers?
Why my Authorization value not passed through Request Headers?

Time:11-27

I am try to send my token value through the config with my post request but it doesnt appear in request header ultimately giving me 500 error Here, is the following code:



export const moneyTransfer = (id, account, amount) => async ( dispatch, getState) => {
  try {
    dispatch({
      type: MONEY_TRANSFER_REQUEST,
    });

    const {
      userLogin: { userInfo },
    } = getState();

    const config = {
      headers: {
        Authorization: `Bearer ${userInfo.token}`,
      },
    };
    console.log(config);
    const { data } = await axios.post(
      `http://localhost:6969/v1/api/transferToAccountByAccountNo?senderAccountNo=${id}&receiverAccountNo=${account}&amount=${amount}`,config );
    console.log(data);
    dispatch({
      type: MONEY_TRANSFER_SUCCESS,
    });
  } catch (error) {
    dispatch({
      type: MONEY_TRANSFER_FAIL,
      payload:
        error.response && error.response.data.message
          ? error.response.data.message
          : error.message,
    });
  }
};

But I cant see it in my chrome request Headers:

POST /v1/api/transferToAccountByAccountNo?senderAccountNo=5&receiverAccountNo=2&amount=123 HTTP/1.1
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-IN,en-US;q=0.9,en;q=0.8
Connection: keep-alive
Content-Length: 103
Content-Type: application/json
Host: localhost:6969
Origin: http://localhost:3000
Referer: http://localhost:3000/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36
sec-ch-ua: "Google Chrome";v="107", "Chromium";v="107", "Not=A?Brand";v="24"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"

CodePudding user response:

This is because, In Axios POST API 2nd parameter is the body object, and 3rd parameter is the config.

So, If you pass a null or empty object in the 2nd parameter, and move the config to the 3rd parameter, it would work

 const { data } = await axios.post(
  `http://localhost:6969/v1/api/transferToAccountByAccountNo?senderAccountNo=${id}&receiverAccountNo=${account}&amount=${amount}`,{}, config );
  • Related