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

React Async/Await Get data with request body

Im trying to get data that requires request body and am having trouble. With other post requests, {requestedThings} worked fine but with get request, it gives me an error

Can someone help me how to fix this ?

async function initSurvey(userNo: number) {
  return await loginApi.get(`/user/survey`, { userNo: userNo });
}

enter image description here

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

I am getting below message :

TS2345: Argument of type ‘{ userNo: number; }’ is not assignable to parameter of type ‘AxiosRequestConfig’.
Object literal may only specify known properties, and ‘userNo’ does not exist in type ‘AxiosRequestConfig’.
46 | async function initSurvey(userNo: number) {

47 | return await loginApi.get(/user/survey, { userNo: userNo });
| ^^^^^^^^^^^^^^
48 | }
49 |
50 | export {

>Solution :

get request method does not have request body according to MDN specs. If you are using axios you should do as described below or send request without body according to your loginApi API.

await axios.get('/user/survey', {
    params: {
      userNo
    }
)
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