0

I am using React in the frontend and Django in the backend and I am currently doing the API integration for which I am using Axios.

So in the frontend side I am calling a URL like this

http://localhost:3000/attempt/?quiz_id=6

and I want the quiz_id = 6 so that I can use this afterward to access data in the backend.

I am using Functional components if that helps.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • Does this answer your question? [Capturing URL parameters in request.GET](https://stackoverflow.com/questions/150505/capturing-url-parameters-in-request-get) – Joan Albert Sep 16 '21 at 08:53

3 Answers3

0

You can take a look at axios's documentation in github

If what you want to do is a GET request this should be enough:

axios.get('http://localhost:3000/attempt/?quiz_id=6')
  .then((res) => {
    // handle success
    console.log(res);
  })

Also you have available this kind of usage:

axios.get('http://localhost:3000/attempt', {
  params: {
    quiz_id: 6
  }
}).then((res) => {
   // handle success
   console.log(res);
  });
Joan Albert
  • 644
  • 10
  • 23
  • Thanks, but I want the quiz_id from the URL, I know how to send the get request. – curiousVaibhav Sep 15 '21 at 09:10
  • Oh I understand now, the question is redacted very very poorly, note everybody understood the same as me! – Joan Albert Sep 15 '21 at 22:18
  • This question is duplicated, you have hundreds of answers about this, for example https://stackoverflow.com/questions/150505/capturing-url-parameters-in-request-get ... – Joan Albert Sep 15 '21 at 22:19
0

You can use this too:

const data =  await axios.get('http://localhost:3000/attempt/?quiz_id=6').then(res => res.data);
0

You can include dynamic quiz_id by using params. The way for this is to making an axios request using the id of the param that you want to update and return:

Your code will read like so:

axios.get('http://localhost:3000/attempt/${quiz_id}', quiz_id);
SohailAQ
  • 1,002
  • 1
  • 13
  • 25