Sending data with token does not work (POST request)

Viewed 30

I send a data with Post request, but error is displayed for me (this line code : console.log(e); I gave the ‍‍token manually to test the code It works for me in the postman, but the code below doesn't work for me:

  const sendDataTab1 = async (e) => {
    const token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRgfjpIjoiNTY0NDhkMGU3NjcwNDcxM2JmN2RlNWUzNmZkZTc1OTI3OGI4MjdiNmJkY2E1MzhhODczMWQ0MDM3MDkzZDM2OGRiNmEzOTNjMzc4MDk4YzYiLCJpYXQiOjE2NjIyNzI5OTguNTE5NTg3LCJuYmYiOjE2NjIyNzI5OTguNTE5NTk2LCJleHAiOjE2OTM4MDg5OTguNTA2OTEzLCJzdWIiOiIxIiwic2NvcGVzIjpbImFkbWluIl19.ehC2JiOfBihEr6cji6RLj_vjA4livmc9He58R2hXVML-YRCRgfqglkPaMT5QihAURlGXlOiuJUNKJDHK9EyIWHVB7MOqJyt4fKJaZZT4xVPsiuA2zWXv3H7u4EMba3sUfkydnWewORBncR2lj-0Qt7wHazwL6Z_7V1A0pZWgVYpm6ndfewnwX44RlxwSRrXO9lRCVTmyYUPa_HqJztXNwf5nvRkw5-u8YaVLACLJs5_LaBQ5Vl5CyRHpeZU--fm27IUz_Qh-S0YQVAeZs1FdpvKXLXf2-quyYYtG_2Mx67i4p4QWeqTPcBZRAsHBD_FTA02eUCqXkHCfy_iDYIGcB7nvBsgKO3G_5bb44nfJyJBnGdJDT49GKh8jlJwM4uA-rbwqF8kT-pN2k28cYmx1v8NNKE5y4AW2APDjAKhUGi2lQEi4mlXrFwO7xDEDfM2k2i8ixy9QsTsPMWEThTCwaLF9TW_qDQZ_Z_3LPfzabIAZt7zlZN6ykl5kuT30hS7pIwZ1Z7xJZCwYGzhapoB6I6KtZ5Up65l0aBLIqh2hNPnpOh612BpcjjOfBWAMXwDH9reWRFoNVHnsYBsXDDW_cPGBEnoVKdzf_lbILLXtxETVB5BURHVfZDFmUvOxY5-kf1jHngJ2HKPOOJknuO19G1bVJLU7MChXih6LOAd1cI0"
    try {
      const api = `https://aitatest.ir/api/v1/test/add`
      await axios.post(api,{ headers: {"Authorization" : `Bearer ${token}`} });
      console.log("hihihi");
    } catch (e) {
      toast.error(e.response.data.error, { autoClose: 15000 });
      console.log(e);
    }
  };
1 Answers

When you post via axios, second param is the data, and third param is the config (https://axios-http.com/docs/post_example)

Therefore you are sending your headers as POST data. Maybe try:

await axios.post(api,{}, { headers: {"Authorization" : `Bearer ${token}`} });
Related