0

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.

Amir Meyari
  • 573
  • 4
  • 9
  • 30

3 Answers3

1

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);
Renaldo Balaj
  • 2,390
  • 11
  • 23
  • may I ask you to have a look at an `axios` related question here : https://stackoverflow.com/questions/59470085/vue-js-validation-fails-for-file-upload-in-axios-when-multipart-form-data-used ? – Istiaque Ahmed Dec 24 '19 at 14:32
  • you have some answers in there, tell me if none worked – Renaldo Balaj Dec 24 '19 at 15:07
1

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);
George
  • 36,413
  • 9
  • 66
  • 103
-1

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);
Kevin.a
  • 4,094
  • 8
  • 46
  • 82