Nodejs 模拟post请求,但是后台得不到参数,用空的帮我看一下

Nodejs 模拟post请求,但是后台得不到参数,用空的帮我看一下

var options = { hostname: ‘localhost’, port: 8080, path: ‘/index/login’, method: ‘POST’ }; var qs=require(‘querystring’); var post_data={ userId:“langzi”, password:‘456465’ };//这是需要提交的数据 var content=qs.stringify(post_data); var req = http.request(options, function(res) { 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(content); req.end();

试了好几种语言,python ,php  在获取post的参数是都没有,得不到userId和password

搞不懂为啥nodejs的api没有设置header的选项呢,问题解决了,谢谢诸位帮忙,问题是这样解决滴: 供需要的人参考 var post_data={ username:userName, password:userPassword, confirmpassword:userPassword }; json这么写是木有问题滴,经过测试可以, 关键在于headers中设置: ‘Content-Type’: ‘application/x-www-form-urlencoded’, ‘Content-Length’: content.length, 好像少哪一个也不可以,content对应的是 var content=qs.stringify(post_data); 开始的时候搞错了,一直在用post_data.length纠结浪费了我好久, 再次谢谢诸位!!


6 回复

根据你提供的信息,我将详细解释如何使用 Node.js 发送 POST 请求,并确保后台能够正确接收到参数。关键在于设置正确的 Content-TypeContent-Length 头信息。

示例代码

const http = require('http');
const qs = require('querystring');

// 需要提交的数据
var post_data = {
    userId: "langzi",
    password: "456465"
};

// 将数据转换为查询字符串格式
var content = qs.stringify(post_data);

// 设置请求选项
var options = {
    hostname: 'localhost',
    port: 8080,
    path: '/index/login',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded', // 设置 Content-Type
        'Content-Length': Buffer.byteLength(content) // 设置 Content-Length
    }
};

// 创建请求对象
var req = http.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function(chunk) {
        console.log('Response: ' + chunk);
    });
});

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

// 写入请求体
req.write(content);

// 结束请求
req.end();

解释

  1. 导入模块:

    • http: Node.js 的内置模块,用于处理 HTTP 请求。
    • qs: 用于将对象转换为查询字符串。
  2. 定义 POST 数据:

    • post_data 是一个包含用户名和密码的对象。
  3. 转换为查询字符串:

    • 使用 qs.stringify(post_data) 将对象转换为 URL 编码的字符串。
  4. 设置请求选项:

    • hostname, port, path, method: 这些是基本的 HTTP 请求配置。
    • headers: 设置 Content-TypeContent-Length 头信息。Content-Type 告诉服务器发送的数据类型,而 Content-Length 告诉服务器发送的数据长度。
  5. 创建请求对象:

    • http.request(options, callback) 创建一个 HTTP 请求对象。
  6. 处理响应:

    • res.on('data', callback) 监听响应数据并打印到控制台。
  7. 错误处理:

    • req.on('error', callback) 监听请求错误并打印错误信息。
  8. 写入请求体:

    • req.write(content) 将请求体写入请求对象。
  9. 结束请求:

    • req.end() 结束请求。

通过这些步骤,你可以确保你的 POST 请求在 Node.js 中被正确发送,并且后台服务器能够接收到参数。


<code>var post_data={ userId:“langzi”, password:‘456465’ };<code> 你这不是json数据 是一个对象了 修改成 <code>var post_data={ “userId”:“langzi”, ”password“:‘456465’ };<code>

在options中加上请求头:

options.headers = {
  'content-type': 'application/x-www-form-urlencoded'
}

谢谢你的建议,问题已解决

参考你的建议问题已解决,不过还要加一句’Content-Length’: content.length,谢谢

根据你的描述,问题出在HTTP请求的头部信息设置不完整。当你使用http.request()方法发送POST请求时,需要正确设置Content-TypeContent-Length头部信息,以确保服务器端能够正确解析请求体中的数据。

示例代码

以下是修正后的代码:

const http = require('http');
const qs = require('querystring');

// 需要提交的数据
var post_data = {
    userId: "langzi",
    password: '456465'
};

// 将对象转换为查询字符串
var content = qs.stringify(post_data);

// 设置请求选项
var options = {
    hostname: 'localhost',
    port: 8080,
    path: '/index/login',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded', // 设置Content-Type
        'Content-Length': Buffer.byteLength(content) // 设置Content-Length
    }
};

// 创建请求
var req = http.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function(chunk) {
        console.log('BODY: ' + chunk);
    });
});

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

// 写入请求体
req.write(content);

// 结束请求
req.end();

解释

  1. 设置Content-Type:

    • Content-Type: application/x-www-form-urlencoded表示请求体是以表单形式编码的数据。
  2. 设置Content-Length:

    • Content-Length表示请求体的长度。这里使用Buffer.byteLength(content)来获取内容的实际长度。
  3. 发送请求:

    • 使用req.write(content)将内容写入请求体。
    • 使用req.end()结束请求。

以上就是完整的解决方案,确保在发送POST请求时正确设置了头部信息。希望这能帮助到你。

回到顶部