javascript - node/fastify 不支持的媒体类型出现错误:application/x-www-form-urlencoded
问题描述
index.js
fastify.get("/paynow", (request, reply) => {
let data = {
TXN_AMOUNT: '10', // request amount
ORDER_ID: 'ORDER_123455', // any unique order id
CUST_ID: 'CUST_1238w4' // any unique customer id
}
// create Paytm Payment
paytm.createPayment(config, data, function (err, data) {
if (err) {
// handle err
}
//store the url and checksum
let url = data.url;
let checksum = data.checksum;
// delete it from data object
delete data.url;
delete data.checksum;
/* Prepare HTML Form and Submit to Paytm */
reply.type('text/html');
reply.raw.writeHead(200, { 'Content-Type': 'text/html' });
reply.header('Content-Type', 'application/x-www-form-urlencoded');
reply.header("Access-Control-Allow-Origin", "*");
reply.header("Access-Control-Expose-Headers", "Content-Range");
reply.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
reply.raw.write('<html>');
reply.raw.write('<head>');
reply.raw.write('<title>Merchant Checkout Page</title>');
reply.raw.write('</head>');
reply.raw.write('<body>');
reply.raw.write('<center><h1>Please do not refresh this page...</h1></center>');
reply.raw.write('<form method="post" action="' + url + '" name="paytm_form">');
for (var x in data) {
reply.raw.write('<input type="hidden" name="' + x + '" value="' + data[x] + '">');
}
reply.raw.write('<input type="hidden" name="CHECKSUMHASH" value="' + checksum + '">');
reply.raw.write('</form>');
reply.raw.write('<script type="text/javascript">');
reply.raw.write('document.paytm_form.submit();');
reply.raw.write('</script>');
reply.raw.write('</body>');
reply.raw.write('</html>');
reply.raw.write('ok')
reply.raw.end();
});
});
fastify.post('/true', (request, reply) => {
console.log('*')
paytm.validate(config, request.body, function (err, data) {
console.log(data)
if (err) {
// handle err
console.log(err)
}
if (data.status == 'verified') {
// mark payment done in your db
}
});
});
Paynow API 工作正常,但是当它重定向到 /true 路由时会出错
{"statusCode":415,"code":"FST_ERR_CTP_INVALID_MEDIA_TYPE","error":"Unsupported Media Type","message":"Unsupported Media Type: application/x-www-form-urlencoded"}
我认为它与 fastify 有关真的不知道,因为这是我第一个使用 fastify 的项目。任何帮助将被应用
解决方案
Fastify 开箱即用,仅解析application/json
有效负载。
要管理表单,您需要将这些插件添加到您的项目中
- fastify-formbody来管理
application/x-www-form-urlencoded
- fastify-multipart来管理
multipart/form-data
(又名文件上传)
推荐阅读
- pytorch - 如何有效地计算 PyTorch 中的混淆矩阵?
- mysql - 解释类型“全部”。无缘无故不在内部连接上使用索引
- c++ - 莱文斯坦距离,多路径
- java - Spring Boot 2 和 OAuth2 客户端凭据不受支持的授权类型
- javascript - 将变量分配给表单字段值
- xamarin.forms - ArcGis 地图不会在导航 Xamarin.Forms 中消失
- c# - 如何使用 xamarin 表单中的会话调用 REST API?
- asp.net - 请求被中止:无法创建 SSL/TLS 安全通道
- java - 无法在 JFrame 上绘画
- ssl - IBM MQ:建立 SSL 连接