如何将 cURL 转换为 axios 请求 [英] How to convert cURL to axios request

查看:39
本文介绍了如何将 cURL 转换为 axios 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我显然在这里忽略了一些东西:

I'm obviously overlooking something here:

我正在尝试将 cURL 请求从 这里.

I am trying to convert a cURL request to axios from Here .

curl -d "grant_type=client_credentials
&client_id={YOUR APPLICATION'S CLIENT_ID} 
&client_secret={YOUR APPLICATION'S CLIENT_SECRET}" 
https://oauth.nzpost.co.nz/as/token.oauth2

这很好用(当我输入凭据时)

This works fine ( when I put my credentials in )

我尝试了以下代码:

import axios from "axios";

async function testApi() {
  try {
    const b = await axios.post("https://oauth.nzpost.co.nz/as/token.oauth2", {
      client_id: "xxxxxxxxxxxxxxxxxxxxxxxxx",
      client_secret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      grant_type: "client_credentials"
    });
  } catch (error) {
    console.log(error);
  }
}

testApi();

这失败了.错误 400.grant_type 是必需的.我试过把它作为参数,包含在 data: json block 中.我想不通!

This fails. Error 400 . grant_type is required. I have tried putting it as a parameter , enclosing within a data: json block . I can't figure this out!

推荐答案

我修复了它,我需要将值放在参数中

I fixed it , I needed to put the values in parameters

import axios from "axios";

async function testApi() {
  try {
    const b = await axios.post("https://oauth.nzpost.co.nz/as/token.oauth2",
        params: {
          client_id: "xxxxxxxxxxxxxxxxxxxxxxxxx",
          client_secret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
          grant_type: "client_credentials"
        });
  } catch (error) {
    console.log(error);
  }
}

testApi();

这篇关于如何将 cURL 转换为 axios 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆