Amir Meyari
Amir Meyari

Reputation: 679

how to set axios post parameters as object?

when the SMS is sent using the API in template literal way works smoothly:

axios.post(
     `https://api.fooserver.com/${API_KEY}/verify/lookup?receptor=${phone}&token=${code}`
    )
     .then(resp => resp.data)

whats wrong with the object param?

axios.post(`https://api.kavenegar.com/v1/${API_KEY}/verify/lookup`, {
            receptor: phone,
            token: code
        })
        .then(resp => resp.data);

it does send request but the object params.

Upvotes: 0

Views: 422

Answers (3)

George
George

Reputation: 36784

In the first example, you are sending the data as query parameters, which isn't the same as sending it in the post body, as in the second example.

You can in fact pass your query parameters as an object, you just need to call .post a little differently:

axios
    .post(
        `https://api.fooserver.com/${API_KEY}/verify/lookup`,
        {},
        {
            params: {
                receptor: phone,
                token: code
            }
        }
        )
    .then(resp => resp.data);

Or, if you so desire:

axios({
    method: 'POST',
    url: `https://api.fooserver.com/${API_KEY}/verify/lookup`,
    params: {
        receptor: phone,
        token: code
    }
})
.then(resp => resp.data);

Upvotes: 1

Renaldo Balaj
Renaldo Balaj

Reputation: 2440

Lucky I understood your question:), using params Axios will automaticity translate your object in query params. Use this:

axios.post(`https://api.kavenegar.com/v1/${API_KEY}/verify/lookup`,{}, {
        params: {
            receptor: phone,
            token: code
        }})
        .then(resp => resp.data);

Upvotes: 1

Kevin.a
Kevin.a

Reputation: 4296

You'll need to use querystring.stringify

Like this :

 const querystring = require('querystring');
    axios.post(`https://api.kavenegar.com/v1/${API_KEY}/verify/lookup`, querystring.stringify({
                receptor: phone,
                token: code
            })
            .then(resp => resp.data);

Upvotes: -1

Related Questions