How to pass params in axios PUT request?
Asked Answered
D

3

6
axios.put('http://localhost:3000/api/userDatas/findUserAddProp',{
    params: {
        userId: "5bb8c3bd16bf1f515ce5b42f",
        prop: "questions",
        questionId: "5bb8c466cb49d8421c05eeda"
    }
});

Server

Userdatas.findUserAddProp = function(req, res, cb) {
    const queryId = req.query.userId;
    const propToUpdate = req.query.prop;
    const questionId = req.query.questionId;

    console.log("queryId: ", queryId);
    console.log("propToUpdate: ", propToUpdate);
    console.log("questionId: ", questionId);
    ...
}

Here is the the server output on the console.

queryId:  undefined
propToUpdate:  undefined
questionId:  undefined

Why is that happening i just passed all params to the server?

Disepalous answered 6/10, 2018 at 14:37 Comment(2)
Please check console.log(req.body)Theatricals
Did you solve the problem?Peradventure
A
3

the put method expects an extra parameter "data" (sent in request body):

axios.put(url[, data[, config]])

pass it as null and you're done:

axios.put('http://localhost:3000/api/userDatas/findUserAddProp', **null,** {
...
Animato answered 11/3, 2019 at 16:5 Comment(0)
E
3

You can open Axios Interface

put<T = any, R = AxiosResponse<T>>(url: string, data?: any, config?: AxiosRequestConfig): Promise<R>;

The Put method recieve the config after the data so you can do:

axios.put('http://localhost:3000/api/userDatas/findUserAddProp',
  // Add null here in the data,
   null,
   // then you can give params in the config 
  { params: { /* your params goes here */ }
});
Elbertelberta answered 14/7, 2022 at 15:27 Comment(0)
A
0

Did you try

req.params.userId

req.param is used to read Url Params like /user/:id whereas req.query is used to read query parameters like http:localhost:8080/api/users?id=123

Asterisk answered 6/10, 2018 at 14:52 Comment(2)
Thank you @ SaqibHussain. I think your explanation and your suggestion contradicts.Disepalous
can you share how you're handling this request on the server side?Asterisk

© 2022 - 2024 — McMap. All rights reserved.