Node.js 请求库——将文本/xml 发布到正文?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19059997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js request library -- post text/xml to body?
提问by Yablargo
I am trying to setup a simple node.js proxy to pass off a post to a web service (CSW in this isntance).
我正在尝试设置一个简单的 node.js 代理以将帖子传递给 Web 服务(在此实例中为 CSW)。
I'm posting XML in a request body, and specifying text/xml. -- The service requires these.
我在请求正文中发布 XML,并指定 text/xml。-- 服务需要这些。
I get the raw xml text in the req.rawBody var and it works fine, I can't seem to resubmit it properly however.
我在 req.rawBody 变量中得到原始 xml 文本并且它工作正常,但是我似乎无法正确地重新提交它。
My method looks like:
我的方法看起来像:
app.post('/csw*', function(req, res){
console.log("Making request to:" + geobusOptions.host + "With query params: " + req.rawBody);
request.post(
{url:'http://192.168.0.100/csw',
body : req.rawBody,
'Content-Type': 'text/xml'
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
}
);
});
I just want to submit a string in a POST, using content-type text/xml. I can't seem to accomplish this however!
我只想在 POST 中提交一个字符串,使用内容类型 text/xml。然而,我似乎无法做到这一点!
I am using the 'request' library @ https://github.com/mikeal/request
我正在使用“请求”库@ https://github.com/mikeal/request
Edit -- Whoops! I forgot to just add the headers...
编辑 - 哎呀!我忘了添加标题...
This works great:
这很好用:
request.post(
{url:'http://192.168.0.100/csw',
body : req.rawBody,
headers: {'Content-Type': 'text/xml'}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
}
);
回答by Yablargo
Well, I sort of figured it out eventually, to repost the body for a nodeJS proxy request, I have the following method:
好吧,我最终想通了,要重新发布 nodeJS 代理请求的正文,我有以下方法:
request.post(
{url:'http://192.168.0.100/csw',
body : req.rawBody,
headers: {'Content-Type': 'text/xml'}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
}
);
I get rawbody by using the following code:
我使用以下代码获取 rawbody:
app.use(function(req, res, next) {
req.rawBody = '';
req.setEncoding('utf8');
req.on('data', function(chunk) {
req.rawBody += chunk;
});
req.on('end', function() {
next();
});
});

