Sam D
Sam D

Reputation: 13

Multiple URL http post request with Axios or any other method

I am looking to post multiple url to my server using http post method, i can not seems to find a solution to that.

Is it possible to do that with Axios http client or just simply making an Http Post Call ?

Axios Post request

axios({
  method: 'post',
  url: 'www.myurl.com',
  dataType: "json",
  contentType: "application/json",

 beforeSend: function (xhr) {
  xhr.setRequestHeader('Authorization', 'Bearer HkwWTBXekkrR0MybFlnUmc9');

  },
     success: function (data) {
     console.log(data);
});

AJAX call method that i have tried that fails

Multiple url in same ajax call?

var urls = ["http://www.test.com/users/", "http://www.example.com/users/", "http://www.test.org/users/"]
$.each(urls, function(index, value) {
                $.ajax({
                    url: value,
                    type: "POST",
                   data: (
            answer_service: answer, expertise_service: expertise, email_service: email),
                    beforeSend: function (xhr) {
              xhr.setRequestHeader('Authorization', 'Bearer 5YCwxCbWEvHIHO_nRvURIG5hP7s');
                    },
                    dataType: "json",
                    contentType: "application/json",
                    success: function (data) {
                    console.log(data);

Upvotes: 0

Views: 600

Answers (1)

ksankar
ksankar

Reputation: 485

Axios has an axios.all method which can handle multiple requests. (https://github.com/axios/axios)

const urls = [
  "https://www.test.com/users/", 
  "https://www.example.com/users/", 
  "https://www.test.org/users/"
];
const generateRequests = () => urls.map( url => axios.get(url));

axios.all(generateRequests())
  .then(axios.spread(function (acct, perms) {
    // All requests are now complete
    console.log('done', acct, perms);
}));

Few more examples:

Upvotes: 1

Related Questions