NodeJS发布请求不返回正文

时间:2017-06-26 14:23:23

标签: javascript node.js request

我正在做一个" GET"请求从URL获取会话ID然后发送" POST"请求所需的表格数据,结果应该返回html正文,但它没有。我根本没有JS的经验,并试图自己做一些事情,但无法弄清楚出了什么问题......我能够从POST请求中打印出标题但是身体仍然是空的(似乎它挂起)

还尝试使用requests模块引发socket hang up错误。

var querystring = require('querystring');
var http = require('http');

var conn_cookie;

var uri = "my_url.com";

var options_get = {
    host: uri,
    path: '/index.php',
    headers: {
        "User-Agent":  "Request"
    }
};

var postData = querystring.stringify({
    cert_num: "xxx",
    car_num: "yyy",
    answer: "0"
});

var options_post = {
    host: uri,
    method: 'POST',
    path: '/index.php',
    headers: {
        "User-Agent":  "Request",
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(postData)
    }
};

var post_req = http.request(options_post, function(res) {
    console.log("POST STATUS: " + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');

    // Nothing is happening here !!
    res.on('data', function (chunk) {
        console.log('Response: ' + chunk);
    });
});
post_req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
});

http.get(options_get, function(res) {
    conn_cookie = res.headers['set-cookie'];

    if (res.statusCode === 200) {
        //options_post['headers']['set-cookie'] = conn_cookie;
        console.log("SENDING POST DATA");

        post_req.write(postData);
        post_req.end();
    }
}).on('error', function(e) {
    console.log("Got error: " + e.message);
});

2 个答案:

答案 0 :(得分:0)

发布请求正在发送正文,您需要设置http请求正文大小。

将此添加到您的帖子标题

 'Content-Length': Buffer.byteLength(post_data)

另外,将var post_data移至var options_post

之前

缓冲区是全局的,因此您不需要它

答案 1 :(得分:0)

我尝试了你的代码,通过创建一个示例服务器并尝试向服务器发送一个帖子请求,我的下面的代码,它适用于我,在你的GET调用的情况下,你可以测试post_req.end()实际上是是否打电话,

var querystring = require('querystring');
var http = require('http');
var post_data = querystring.stringify({
 name: "Jack",
 surname: "Daniels",
 answer: "0"
 });

var options_post = {
  host: 'localhost',
  method: 'POST',
  path: '/test/data',
  port:9008,
  headers: {
    connection: 'keep-alive',
    accept: 'text/html',
    'Content-Type': 'application/x-www-form-urlencoded',
    "User-Agent":  "Mozilla/5.0",
     'Content-Length': Buffer.byteLength(post_data)
  }
};

 var post_req = http.request(options_post, function(res) {
   console.log("POST STATUS: " + res.statusCode);
   console.log('HEADERS: ' + JSON.stringify(res.headers));
   res.setEncoding('utf8');

  // Nothing is happening here !!
    res.on('data', function (chunk) {
     console.log('Response: ' + JSON.stringify(chunk));
    });
  });
   post_req.on('error', function(e) {
   console.log('problem with request: ' + e.message);
 });
 post_req.write(post_data);
 post_req.end();

这是我的服务器代码:

var bodyParser = require('body-parser');
var express=require('express');
var app = express();
var port =  9008;


// Start the server

app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({extended: true})); // support encoded bodies

app.post("/test/data",function(req,res){

var body=req.body;
res.send("ok"+JSON.stringify(body));
});
//start the server
app.listen(port);
console.log('Server has started!! ' + port);

我得到了输出:

HEADERS: {"x-powered-by":"Express","content-type":"text/html; charset=utf-8","content-length":"50","etag":"W/\"32-ml7xbrRwyj1GKXj
cJAYZOcmEm2s\"","date":"Mon, 26 Jun 2017 17:12:23 GMT","connection":"keep-alive"}
Response: "ok{\"name\":\"Jack\",\"surname\":\"Daniels\",\"answer\":\"0\"}"

request npm模块更容易进行POST / GET请求,不用担心错误处理和请求关闭以及打开模块处理所有内容。