我想将一些帖子数据发送到api
10.11.12.13/new/request/create
这是一个在门户网站中创建新请求的API。现在我在NodeJs中创建一个应用程序,并希望从节点js应用程序创建请求。 现在我必须发送这种格式
{"user":"demo", "last_name":"test","contact":"989898989"}
所以如何在上面的url上发送数据来创建新请求。
我是NodeJ的初学者,并且没什么好主意的。 将不胜感激任何帮助。
提前致谢
答案 0 :(得分:1)
我建议使用axios
或任何其他请求lib:
const axios = require('axios');
axios.post('10.11.12.13/new/request/create', {
user: 'demo',
last_name: 'test',
contact: '989898989',
});
答案 1 :(得分:1)
这是使用请求模块
的示例 var headers = {
'Content-Type': 'application/json'
}
var options = {
url: "10.11.12.13/new/request/create" ,
method: 'POST',
headers: headers,
json: true,
body: {user:"demo", last_name:"test",contact:"989898989"}
}
request(options, function (error, response, body) {
if (error) {
//do something
}
console.log(body)//do something with response
})
答案 2 :(得分:0)
您可以使用您的网址和正文(您想要发布)使用postman
REST客户端进行GET方法,然后点击 * Code * 并选择NodeJS,他们会找到代码生成供您使用。这是链接https://www.getpostman.com/docs/postman/sending_api_requests/generate_code_snippets
根据我的经验,最好从节点js的 Request 包开始。以下是供您参考的链接:https://www.npmjs.com/package/request