将邮递员api调用转换为Node.js调用 [英] Convert postman api call to Node.js call

查看:98
本文介绍了将邮递员api调用转换为Node.js调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试向使用swagger 2.0制作的API发出发布请求时遇到麻烦(不是我本人)。

I'm getting some troubles when trying to make a post request to an API made with swagger 2.0 (not by me).

我已经导入了一个集合邮递员,当我执行邮寄请求时,它可以完美地工作。但是,在Node.js中,使用swagger库输出400错误,使用axios输出500错误。

I've imported a collection to postman, and when i perform a post request it works perfect. However in Node.js it outputs a 400 error with swagger library, and a 500 with axios.

此处显示了集合在邮递员中提供的架构:

Heres the schema that the collection provides in postman:

{
  "workflowFunctionID": 1,
  "workflowActionParameters": [
    {
      "name": "Description",
      "value": "Probando y wea2",
      "workflowFunctionParameterId": 2
    },
    {
        "name": "Price",
        "value": "25000",
        "workflowFunctionParameterId": 3
    }
  ]
}

正如我提到的那样,它运行非常完美。这是使用Node.js的当前代码:

As i mentioned it works perfectly. And this is the current code that am using Node.js:

main = async() => {
  try {
    const token = await acquireTokenWithClientCredentials(RESOURCE, CLIENT_APP_Id, CLIENT_SECRET, AUTHORITY);

    const request = {
      url: `${WORKBENCH_API_URL}/api/v1/contracts?workflowId=1&contractCodeId=1&connectionId=1`,
      method: "POST",
      headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token.access_token}` },
      body: {
        workflowActionInput: {
          workflowFunctionID: 1,
          workflowActionParameters: [{
              "name": "description",
              "value": "cualkier wea"
            },
            {
              "name": "price",
              "value": "20000000"
            }
          ]
        }
      }
    }

    let res = await Swagger.http(request);

    console.log(res);
  }
  catch (err) {
    console.error(err);
  }
}

main();

我应如何将正文/表单数据传递给发布请求,或者使用其他软件包或码?预先感谢您的帮助。

How should i pass the body/form-data to the post request, or maybe using another package or code? Thanks in advance for any help.

推荐答案

在邮递员中运行api时,只需看到我标记为 code的按钮黑色

When you have api running in postman, just see this button named "code" i marked with black


  • 单击此按钮

  • 选择语言为node.js

它将在node.js中显示该API的代码,只需复制该代码并粘贴到需要的位置即可。
在这里,我要附加图片,请参见

It will show you code in node.js for that api, just copy that code and paste where required. Here i am attaching picture kindly see this

这篇关于将邮递员api调用转换为Node.js调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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