0

I'm trying to figure out why I can't make a post request using react-query. Basically, I try to write the code below, which is working, using react-query:

//makeRequest implementation

import axios from "axios";

export const makeRequest = axios.create({
  baseURL: process.env.REACT_APP_BASE_URL,
  withCredentials: false,
});

const today = moment();
  const currentMonth = today.format("MM");
  const currentYear = today.format("YYYY");
  const date = `${currentYear}-${currentMonth}`;

//this code is working

const [tours, setTours] = useState([]);
useEffect(() => {
    const fetchData = async () => {
      try {
        const res = await makeRequest.post(
          "get-tours",
          { data: date },
          { headers }
        );
        console.log(res.data);
        setTours(res.data);
      } catch (err) {
        console.log(err);
      }
    };
    fetchData();
  }, [date]);

//this code return 401 - Unauthorized, the response is 'Unauthenticated.' and I don't understand why

const { isLoading, error, data } = useQuery(["tours"], () =>
    makeRequest
      .post("get-tours", {
        headers,
        data: { data: date },
      })
      .then((res) => res.data)
  );

Can someone, please help me? Any help is appreciated. Thanks!

alexsmt
  • 71
  • 7

0 Answers0