Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

While using GET method, can I convert query string to Object and send to an endpoint?

I’m using Axios for HTTP request and using useState hook for query string value.

const url = `${BASE_URL}/courses?page=${currentPage}&count=${contentCount}&lastContentId=${lastContentId}&search=${searchVal}`

axios.get(url)
.then((res) => console.log(res))

For now, I send every query string inside url variable. However, what I’m trying to do is:

    const url = `${BASE_URL}/courses?`
    const queryObj: any = {
      page: currentPage,
      count: contentCount,
      lastContentId : lastContentId,
      search: searchVal,
    }
    axios
      .get(url, queryObj)
      .then((res) => console.log(res))

convert into this format. However, it is not working.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What I want to know is whether it is possible or not to convert query string to object and how it can be done.

>Solution :

Did you read the docs

Second argument of get is options which have multiple parameters params included

const queryObj: any = {
  page: currentPage,
  count: contentCount,
  lastContentId : lastContentId,
  search: searchVal,
}
axios
  .get(url, { params: queryObj })
  .then((res) => console.log(res))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading