skip to Main Content
const getSchool = async () => {
    const payload = {
        register: "1110000000000",
        id_tingkat_kelas: 11,
        list_id_produk: [
            1, 2, 3, 4, 5, 6, 7,8,9,10, 11, 13, 14, 15, 16
        ]
    };
    
  api.get(`${process.env.NEXT_PUBLIC_API_URL_ORIGIN}/mobile/v18/tobk/ujian-sekolah/all`,
      {
        headers: {
          "Content-Type": "application/json",
          "X-API-KEY": `${process.env.NEXT_PUBLIC_API_KEY}`,
          Authorization: "Bearer " + session?.user?.token,
        },
        data: payload
      }
    )
    .then((res) => {
      setDataLaporan(res?.data?.data);
      setIsLoading(false);
    })
    .catch((err) => { });

};

Can someone help me?

2

Answers


  1. In Axios, the GET method doesn’t have a data option in its config object. Instead, you can use the params option to send data in the request body while making a GET request.

    const getSchool = async () => {
      const payload = {
        register: "1110000000000",
        id_tingkat_kelas: 11,
        list_id_produk: [
          1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16
        ]
      };
    
      try {
        const res = await api.get(`${process.env.NEXT_PUBLIC_API_URL_ORIGIN}/mobile/v18/tobk/ujian-sekolah/all`, {
          headers: {
            "Content-Type": "application/json",
            "X-API-KEY": `${process.env.NEXT_PUBLIC_API_KEY}`,
            Authorization: "Bearer " + session?.user?.token,
          },
          params: payload // Use params instead of data for GET requests
        });
    
        setDataLaporan(res?.data?.data);
        setIsLoading(false);
      } catch (err) {
        console.error(err);
        // Handle error
      }
    };
    Login or Signup to reply.
  2. in axios the GET method does not send a body with the request . you usually send the parameters in the url , however you can change the data option to params . so your code will be like this :

    const getSchool = async () => {
    const payload = {
        register: "1110000000000",
        id_tingkat_kelas: 11,
        list_id_produk: [
            1, 2, 3, 4, 5, 6, 7,8,9,10, 11, 13, 14, 15, 16
        ]
    };
    
    api.get(`${process.env.NEXT_PUBLIC_API_URL_ORIGIN}/mobile/v18/tobk/ujian-sekolah/all`,
        {
            headers: {
                "Content-Type": "application/json",
                "X-API-KEY": `${process.env.NEXT_PUBLIC_API_KEY}`,
                Authorization: "Bearer " + session?.user?.token,
            },
            params: payload 
        }
    )
    .then((res) => {
        setDataLaporan(res?.data?.data);
        setIsLoading(false);
    })
    .catch((err) => { });
    

    };

    you can also switch to another HTTP method like POST to be able to send body in your request

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search