通过POST请求将数据从node.js服务器发送到node.js服务器

通过POST请求将数据从node.js服务器发送到node.js服务器

问题描述:

我正在尝试通过POST请求将数据从node.js服务器发送到另一个node.js服务器.我在客户端" node.js中的操作如下:

I'm trying to send data through a POST request from a node.js server to another node.js server. What I do in the "client" node.js is the following:

var options = {
    host: 'my.url',
    port: 80,
    path: '/login',
    method: 'POST'
};

var req = http.request(options, function(res){
    console.log('status: ' + res.statusCode);
    console.log('headers: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function(chunk){
        console.log("body: " + chunk);
    });
});

req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
});

// write data to request body
req.write('data\n');
req.write('data\n');
req.end();

该块或多或少是从node.js网站上获取的,因此它应该是正确的.我唯一看不到的是如何在options变量中包括用户名和密码以进行实际登录.这就是我处理服务器node.js中的数据的方式(我使用express):

This chunk is taken more or less from the node.js website so it should be correct. The only thing I don't see is how to include username and password in the options variable to actually login. This is how I deal with the data in the server node.js (I use express):

app.post('/login', function(req, res){
    var user = {};
    user.username = req.body.username;
    user.password = req.body.password;
        ...
});

如何将那些usernamepassword字段添加到options变量中以使其登录?

How can I add those username and password fields to the options variable to have it logged in?

谢谢

发布数据是作为请求正文发送查询字符串的方式(就像您在?之后使用URL发送它的方式一样).

Posting data is a matter of sending a query string (just like the way you would send it with an URL after the ?) as the request body.

这需要Content-TypeContent-Length标头,因此接收服务器知道如何解释传入的数据. (*)

This requires Content-Type and Content-Length headers, so the receiving server knows how to interpret the incoming data. (*)

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

var data = querystring.stringify({
      username: yourUsernameValue,
      password: yourPasswordValue
    });

var options = {
    host: 'my.url',
    port: 80,
    path: '/login',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(data)
    }
};

var req = http.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log("body: " + chunk);
    });
});

req.write(data);
req.end();


(*)发送数据需要正确设置Content-Type标头,即application/x-www-form-urlencoded表示标准HTML表单将使用的传统格式.


(*) Sending data requires the Content-Type header to be set correctly, i.e. application/x-www-form-urlencoded for the traditional format that a standard HTML form would use.

以完全相同的方式发送JSON(application/json)很容易;只是JSON.stringify()预先提供的数据.

It's easy to send JSON (application/json) in exactly the same manner; just JSON.stringify() the data beforehand.

URL编码的数据支持一种结构级别(即键和值).在交换具有嵌套结构的数据时,JSON很有用.

URL-encoded data supports one level of structure (i.e. key and value). JSON is useful when it comes to exchanging data that has a nested structure.

最重要的是:服务器必须能够解释有问题的内容类型.可能是text/plain或其他任何内容;如果接收服务器按原样理解数据,则无需转换数据.

The bottom line is: The server must be able to interpret the content type in question. It could be text/plain or anything else; there is no need to convert data if the receiving server understands it as it is.

如果您的数据使用不寻常的字符集(例如,不是UTF-8),请添加一个字符集参数(例如application/json; charset=Windows-1252).例如,如果您从文件中读取它,则可能有必要.

Add a charset parameter (e.g. application/json; charset=Windows-1252) if your data is in an unusual character set, i.e. not UTF-8. This can be necessary if you read it from a file, for example.