Home  >  Article  >  Database  >  How do I send query parameters with Axios in a POST request?

How do I send query parameters with Axios in a POST request?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-01 10:50:02424browse

How do I send query parameters with Axios in a POST request?

Query Parameter Posting with Axios

When making a POST request with Axios, you may encounter a scenario where you need to attach query parameters to the URL. This differs from sending data within the request body.

One common issue is when attempting to pass query parameters with Axios in React Native, resulting in a 400 error due to invalid query parameters.

To resolve this, Axios requires you to specify query parameters differently from request data. While the function signature for post is axios.post(url[, data[, config]]), you'll need to pass the query parameters as the third argument within the config object.

To illustrate, consider the following code:

<code class="javascript">.post(`/mails/users/sendVerificationMail`, null, { params: {
  mail,
  firstname
}})
.then(response => response.status)
.catch(err => console.warn(err));</code>

This code will send a POST request with an empty body and the specified query parameters:

POST http://localhost:8000/api/mails/users/sendVerificationMail?mail=lol%40lol.com&amp;firstname=myFirstName

The above is the detailed content of How do I send query parameters with Axios in a POST request?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn