簡體   English   中英

NodeJS發布請求未返回正文

[英]NodeJS post request doesn't return body

我正在執行“ 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);
});

發布請求正在發送正文,您需要設置http請求正文大小。

將此添加到您的帖子標題

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

另外,將您的var post_data移動到var options_post之前

緩沖區是全局的,因此您不需要

我嘗試了您的代碼,通過創建一個示例服務器並嘗試向我的以下代碼發送后發請求到服務器,它對我有用,在您使用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請求,而不必擔心很多錯誤處理,並且請求關閉和打開模塊可以處理所有事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM