Your goal is to post to an API with specific query parameters, namely mail and firstname. While it works smoothly in tools like PostMan and Insomnia, your React Native app encounters a 400 error indicating invalid query parameters.
To resolve this issue, understand the signature of axios's post method: axios.post(url[, data[, config]]). The crucial step here is to specify the query parameters as part of the third config argument.
Here's the updated code:
<code class="javascript">.post(`/mails/users/sendVerificationMail`, null, { params: { mail, firstname }}) .then(response => response.status) .catch(err => console.warn(err));</code>
By passing an empty body (null) and setting the params property within the config object, you instruct Axios to send the data as query parameters. The resulting POST request will have the expected URL with appended query parameters:
POST http://localhost:8000/api/mails/users/sendVerificationMail?mail=lol%40lol.com&firstname=myFirstName
The above is the detailed content of How to Send Query Parameters with axios.post() and Avoid a 400 Error?. For more information, please follow other related articles on the PHP Chinese website!