问题描述
我在尝试向使用 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.
推荐答案
当你在 postman 中运行 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
这篇关于将 postman api 调用转换为 Node.js 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!