【发布时间】:2015-10-10 15:15:00
【问题描述】:
我正在使用 node.js 将 HTTP POST 发送到 APP 服务器。 POST 正文中的电子邮件地址 joe@gmail.com 在 APP 服务器端显示为 joe%40gmail.com。根据服务器日志,我在以下代码中使用了 Content-Type text/plain,但 application/x-www-form-urlencoded 实际上位于 POST 请求标头中。
我之前的帖子在 Chrome 邮递员结果上有一个错误。请忽略它,并为混淆感到抱歉。
我是新手,可能在这里遗漏了一些明显的东西。任何帮助表示赞赏。
代码如下:
var express = require("express");
var app = module.exports = express();
var request = require('request');
var config = require('../config');
app.post('/', function(req, res) {
var username = req.body.name;
var password = req.body.passwd;
var email = req.body.email;
console.log("email=", email);
// connect to APP server
var url = config.appServer;
request( {
uri: url,
method: "POST",
timeout: 10000, // 10 sec
headers: {
'Content-Type': 'text/plain'
},
form: {
act: 'create',
username : username,
passwd: password,
email: email
}
}, function (error, response, body) {
// handle return here
【问题讨论】:
-
看起来是 url 编码的,你试过
decodeURIComponent()吗? -
看起来这是您的其他应用程序的问题,而不是这个问题,因为
console.log输出了正确的字符串。使用/account后处理程序的代码更新问题 -
不,我没有尝试 decodeURIComponent()。我可以使用 Chrome、Safari 发送具有相同字符串的 HTTP GET 请求。它们都正确显示在 APP 服务器端。我可能错过了 node.js 请求的标头中的某些内容,但不知道是什么。
-
gfpacheco & adeneo,感谢您的 cmets。给定content-type x-www-form-urlencoded,上面的结果是可以理解的。剩下的问题是为什么上面的代码中的标题不正确。我已经更正了前面问题中关于邮递员的错误。
标签: javascript node.js http post httprequest