C# 如何在express node.js POST请求中接收JSON?
我从C#发送了一个POSTC# 如何在express node.js POST请求中接收JSON?,c#,node.js,C#,Node.js,我从C#发送了一个POSTWebRequest,以及一个JSON对象数据,并希望在Node.js服务器中接收它,如下所示: var express = require('express'); var app = express.createServer(); app.configure(function(){ app.use(express.bodyParser()); }); app.post('/ReceiveJSON', function(req, res){ //Suppose
WebRequest
,以及一个JSON对象数据,并希望在Node.js服务器中接收它,如下所示:
var express = require('express');
var app = express.createServer();
app.configure(function(){
app.use(express.bodyParser());
});
app.post('/ReceiveJSON', function(req, res){
//Suppose I sent this data: {"a":2,"b":3}
//Now how to extract this data from req here?
//console.log("req a:"+req.body.a);//outputs 'undefined'
//console.log("req body:"+req.body);//outputs '[object object]'
res.send("ok");
});
app.listen(3000);
console.log('listening to http://localhost:3000');
此外,POSTWebRequest
的C#end可通过以下方法调用:
public string TestPOSTWebRequest(string url,object data)
{
try
{
string reponseData = string.Empty;
var webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
if (webRequest != null)
{
webRequest.Method = "POST";
webRequest.ServicePoint.Expect100Continue = false;
webRequest.Timeout = 20000;
webRequest.ContentType = "application/json; charset=utf-8";
DataContractJsonSerializer ser = new DataContractJsonSerializer(data.GetType());
MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, data);
String json = Encoding.UTF8.GetString(ms.ToArray());
StreamWriter writer = new StreamWriter(webRequest.GetRequestStream());
writer.Write(json);
}
var resp = (HttpWebResponse)webRequest.GetResponse();
Stream resStream = resp.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
reponseData = reader.ReadToEnd();
return reponseData;
}
catch (Exception x)
{
throw x;
}
}
方法调用:
TestPOSTWebRequest("http://localhost:3000/ReceiveJSON", new TestJSONType {a = 2, b = 3});
如何解析上面Node.js代码中请求对象的JSON数据 bodyParser会自动为您执行此操作,只需执行
console.log(req.body)
编辑:您的代码是错误的,因为您首先包含app.router(),然后才包含bodyParser和其他内容。那太糟糕了。您甚至不应该包含app.router(),Express会自动为您添加。下面是代码的外观:
var express = require('express');
var app = express.createServer();
app.configure(function(){
app.use(express.bodyParser());
});
app.post('/ReceiveJSON', function(req, res){
console.log(req.body);
res.send("ok");
});
app.listen(3000);
console.log('listening to http://localhost:3000');
您可以使用Mikeal的nice模块通过发送带有以下参数的POST请求来测试这一点:
var request = require('request');
request.post({
url: 'http://localhost:3000/ReceiveJSON',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
a: 1,
b: 2,
c: 3
})
}, function(error, response, body){
console.log(body);
});
更新:用于express 4+。请求必须与以下内容一起发送: 内容类型:“application/json;charset=utf-8”
否则,bodyParser会将您的对象作为另一个对象中的键踢入:)console.log(req.body)输出[object object];我也尝试了req.body.a,但它打印的是未定义的。我编辑了代码,您的错误是将路由器放在其他中间件(包括bodyParser)之前。hmmm。但是现在是console.log(请求主体);输出{}!如何提取json对象属性a和b?如果您运行我的精确代码,一切正常,我甚至在本地进行了测试:)我有您的精确代码bro。我是否也要显示您的C#webrequest?虽然它确实显示{“a”:2,“b”:3}正在调试中传递。哦,天才!我怎么会错过这个!先生,你刚刚救了我一天