How to post query parameters with Axios?
Asked Answered
L

4

210

I am trying to post on an API with some query params. This is working on PostMan / Insomnia when I am trying to by passing mail and firstname as query parameters :

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

However, when I am trying to do it with my react native app, I got a 400 error (Invalid Query Parameters).

This is the post method :

.post(`/mails/users/sendVerificationMail`, {
  mail,
  firstname
})
.then(response => response.status)
.catch(err => console.warn(err));

(my mail and firstname are console.logged as follow: [email protected] and myFirstName).

So I don't know how to pass Query Parameters with Axios in my request (because right now, it's passing data: { mail: "[email protected]", firstname: "myFirstName" }.

Lau answered 27/11, 2018 at 13:48 Comment(0)
C
400

axios signature for post is axios.post(url[, data[, config]]). So you want to send params object within the third argument:

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

This will POST an empty body with the two query params:

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

Crew answered 27/11, 2018 at 13:56 Comment(4)
how can i send request with params and body with axios?Carmacarmack
@Carmacarmack .post(/mails/users/sendVerificationMail, body, { params: { mail, firstname }}) .then(response => response.status) .catch(err => console.warn(err));Pretended
This doesn't support sending objects, such as a file.Electroencephalograph
@Electroencephalograph why do you say that and why is that relevant to the question?Crew
K
34

As of 2021 insted of null i had to add {} in order to make it work!

axios.post(
        url,
        {},
        {
          params: {
            key,
            checksum
          }
        }
      )
      .then(response => {
        return success(response);
      })
      .catch(error => {
        return fail(error);
      });
Kiosk answered 22/1, 2021 at 16:48 Comment(1)
Year makes no sense. For axios version 0.27.2, null or undefined works as the second parameter. axios.post(url, null, { params: { ... } })Grindstone
E
12

In my case, the API responded with a CORS error. I instead formatted the query parameters into query string. It successfully posted data and also avoided the CORS issue.

        var data = {};

        const params = new URLSearchParams({
          contact: this.ContactPerson,
          phoneNumber: this.PhoneNumber,
          email: this.Email
        }).toString();

        const url =
          "https://test.com/api/UpdateProfile?" +
          params;

        axios
          .post(url, data, {
            headers: {
              aaid: this.ID,
              token: this.Token
            }
          })
          .then(res => {
            this.Info = JSON.parse(res.data);
          })
          .catch(err => {
            console.log(err);
          });
Exploitation answered 13/7, 2020 at 23:55 Comment(0)
V
2

You can use params and body together in a request with axios

  sendAllData (data) {
   return axios
        .post(API_URL + "receiveData", JSON.stringify(data), {
          headers: { "Content-Type": "application/json; charset=UTF-8" },
          params: { mail: [email protected] }, //Add mail as a param
        })
        .then((response) => console.log("repsonse", response.status)); 
  }
Vermination answered 14/12, 2022 at 5:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.