在NodeJS服务器上从Dart接收POST请求时,会给出一个空值为和的JSON字符串



这是我在dart端的代码。我已经创建了一个对象,并尝试使用jsonEncode方法将该对象作为请求主体发送。

class RZPOrder {
final int orderId;
final int amount;
RZPOrder({this.orderId, this.amount});
Map toJson() => {'orderId': orderId, 'amount': amount};
}
String url = 'http://876b98959b91.ngrok.io';
print(jsonEncode(order));
final http.Response res = await http.post(
url,
headers: <String, String>{
'Content-Type': 'application/x-www-form-urlencoded',
},
body: jsonEncode(order),
);

此处输出的打印语句为{"orderId":842,"amount":3500}

在节点服务器端,代码为

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded());
app.route('/')
.post(function(req, res){
console.log(req.body);
var options = {
amount: req.body.amount,
receipt: req.body.orderId
};
});

此处的打印声明是{ '{"orderId":555,"amount":3500}': '' }

在使用邮递员时,我得到了与飞镖相同的输出。

请帮助我在节点端获得正确的json字符串。谢谢

Content-Type更改为省道侧的'application/json'和节点侧的app.use(bodyParser.json());解决了问题。

但是,我仍然不明白为什么在使用x-www-form-urlencoded时在节点端更改了json字符串。

最新更新