【问题标题】:using axios for post call in parallel并行使用 axios 进行 post call
【发布时间】:2020-09-21 11:20:06
【问题描述】:

我有这个正在运行的 axios 请求 ok 我得到了 http 200

import {AxiosResponse} from "axios";

const axios = require('axios').default;
const qs = require('qs');

//First query 
axios({
    method: 'post',
    url: 'https://oauth2.-arch.mand.com/oauth2/token',
    data: qs.stringify({'grant_type': 'client_credentials'}, {'scope': 'run:crud'}),
    headers: {
        'Accept': 'application/json',
        'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',
        'Content-Type': 'application/x-www-form-urlencoded',
    }
}).then(function (response: AxiosResponse) {

    console.log(response.data);
}).catch(function (error: any) {
    console.log("Error: " + error);
});

//第二次查询(唯一不同的是范围)

axios({
    method: 'post',
    url: 'https://oauth2.-arch.mand.com/oauth2/token',
    data: qs.stringify({'grant_type': 'client_credentials'}, {'scope': ‘exe:crud’}),
    headers: {
        'Accept': 'application/json',
        'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',
        'Content-Type': 'application/x-www-form-urlencoded',
    }
}).then(function (response: AxiosResponse) {

    console.log(response.data);
}).catch(function (error: any) {
    console.log("Error: " + error);
});

现在我想并行运行两个请求并得到response.data的结果(我发送到请求/标头等的数据)

我试过了,但我得到的响应就像请求一样,我没有得到令牌/数据?任何想法 如何同时使用这两个请求

axios.all([{
    method: 'post',
    url: 'https://oauth2.-arch.mand.com/oauth2/token',
    data: qs.stringify({'grant_type': 'client_credentials'}, {'scope': 'run:crud'}),
        headers: {
            'Accept': 'application/json',
            'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',
            'Content-Type': 'application/x-www-form-urlencoded',
        }
    },

        {
            method: 'post',
            url: 'https://oauth2.-arch.mand.com/oauth2/token',
            data: qs.stringify({'grant_type': 'client_credentials'}, {'scope': 'exe:crud'}),
            headers: {
                'Accept': 'application/json',
                'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',
                'Content-Type': 'application/x-www-form-urlencoded',
            }
        }]).then(axios.spread((...responses: AxiosResponse[]) =>{

        const aToken = responses[0];
        const bToken = responses[1];
    }).catch(function (error: any) {
        console.log("Error: " + error);
    });

如果有更好的方法,请告诉我

顺便说一句,如果我将这两个帖子请求分开运行,一切正常

【问题讨论】:

  • 如果你稍微改变一下语法会怎样? .then(axios.spread((aToken, bToken) => { // output of req. console.log('aToken', aToken, 'bToken', bToken)})
  • @CristianGabor - 谢谢我已经尝试过了,我得到了相同的结果,这是我放入 axios.all 的两个请求对象。我可以在调试 url 中看到方法等,我想获取令牌...有什么想法吗?
  • @CristianGabor - 谢谢我已经尝试过了,我得到了相同的结果,这是我放入 axios.all 的两个请求对象。我可以在调试 url 中看到方法等,我想获取令牌...有什么想法吗?

标签: javascript node.js promise async-await axios


【解决方案1】:

您需要向axios.all 提供承诺,而不是他们的配置对象。

axios.all([
    axios({
      method: 'post',
      url: 'https://oauth2.-arch.mand.com/oauth2/token',
      data: qs.stringify({
        'grant_type': 'client_credentials'
      }, {
        'scope': 'run:crud'
      }),
      headers: {
        'Accept': 'application/json',
        'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',
        'Content-Type': 'application/x-www-form-urlencoded',
      }
    }),
    axios({
      method: 'post',
      url: 'https://oauth2.-arch.mand.com/oauth2/token',
      data: qs.stringify({
        'grant_type': 'client_credentials'
      }, {
        'scope': 'exe:crud'
      }),
      headers: {
        'Accept': 'application/json',
        'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',
        'Content-Type': 'application/x-www-form-urlencoded',
      }
    })
  ]).then(axios.spread((...responses: AxiosResponse[]) => {
      const aToken = responses[0];
      const bToken = responses[1];
    }).catch(function(error: any) {
      console.log("Error: " + error);
    });

请记住,axios.allaxios.spreaddeprecated

直接使用Promise.all

【讨论】:

  • 非常感谢,如果您能提供一个示例,我应该如何在不弃用的情况下更改代码。谢谢
猜你喜欢
  • 1970-01-01
  • 2019-08-29
  • 2021-05-28
  • 1970-01-01
  • 2017-07-02
  • 2019-10-10
  • 2021-10-15
  • 2018-05-28
  • 2012-10-04
相关资源
最近更新 更多