Convert postman api call to Node.js call

让人想犯罪 __ 提交于 2020-03-25 18:01:43

问题


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

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
    }
  ]
}

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.


回答1:


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

  • click on this button
  • select language as node.js

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



来源:https://stackoverflow.com/questions/54787030/convert-postman-api-call-to-node-js-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!