繁体   English   中英

通过 node.js POST 方法

[英]POST method through node.js

我正在为 REST API 调用编写一个简单的 node.js,以通过 POST 方法创建对象并获取响应代码。 但是在运行脚本时,我得到了“0 通过”。

这是我的代码:

 var request = require("request"); var options = { method: 'POST', url: 'https://example.org', headers: { 'content-type': 'application/vnd.nativ.mio.v1+json', authorization: 'Basic hashedTokenHere' }, //body: '{\\n\\n"name": "My JS TEST",\\n"type": "media-asset"\\n\\n}' }; request(options, function (error, response, body) { if(error) { console.log(error); } else { console.log(response.statusCode, body); } });

任何人都可以帮助成功运行它并获取响应代码吗?

谢谢!

我打电话给我的本地 API 希望这能让事情变得清楚。 这对我有用

这是我的 API

var  express = require('express')
var router = express.Router()
var bodyParser = require('body-parser');
var app=express()
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.post('/api',function(req,res)
        {
    res.status(200).send(req.body.name+"hi");
})
app.listen(8080,function(){
           console.log("start");
           })

现在,通过请求,我将请求这个 post 方法

var request = require('request');
// Set the headers
var headers = {
    'User-Agent':       'Super Agent/0.0.1',
    'Content-type': 'application/json',
     'Authorization': 'Basic ' + auth,
    }
var postData = {
  name: 'test',
  value: 'test'
}
// Configure the request
var options = {
    url: 'http://127.0.0.1:8080/api',
    method: 'POST',
    json: true,
    headers: headers,
    body: postData
}

// Start the request
request(options, function (error, response, body) {
        // Print out the response body and head
        console.log("body = "+body+"  head= "+response.statusCode)
    }
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM