简体   繁体   English

node/fastify 不支持的媒体类型出现错误:application/x-www-form-urlencoded

[英]Giving error with node/fastify Unsupported Media Type: application/x-www-form-urlencoded

index.js 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 is working fine but when it redirects to /true route it giving error 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"} {"statusCode":415,"code":"FST_ERR_CTP_INVALID_MEDIA_TYPE","error":"Unsupported Media Type","message":"Unsupported Media Type: application/x-www-form-urlencoded"}

I think it is related to fastify really don't know as it is my first project with fastify.我认为它与 fastify 有关真的不知道,因为这是我第一个使用 fastify 的项目。 Any help will be appricated任何帮助将被应用

index.js 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 is working fine but when it redirects to /true route it giving error 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"} {“状态代码”:415,“代码”:“ FST_ERR_CTP_INVALID_MEDIA_TYPE”,“错误”:“不支持的媒体类型”,“消息”:“不支持的媒体类型:application / x-www-form-urlencoded”}

I think it is related to fastify really don't know as it is my first project with fastify.我认为它与fastify有关,真的不知道,因为这是我第一个与fastify合作的项目。 Any help will be appricated任何帮助将被申请

For completeness, I needed something other than those two (I needed multipart/related).为了完整起见,我需要这两个以外的东西(我需要多部分/相关)。 You can add your own content type parser using 'addContentTypeParser' on the Fastify instance.你可以在 Fastify 实例上使用 'addContentTypeParser' 添加你自己的内容类型解析器。

https://www.fastify.io/docs/latest/Reference/Server/#addcontenttypeparser https://www.fastify.io/docs/latest/Reference/Server/#addcontenttypeparser

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在节点js中使用参数为application / x-www-form-urlencoded类型的POST调用 - How to consume POST call where parameter is of type application/x-www-form-urlencoded in node js 新的 FormData() “application/x-www-form-urlencoded” - new FormData() "application/x-www-form-urlencoded" 使用application / x-www-form-urlencoded使用node.js在发布请求中发送数组 - Send Array in post request using node.js using application/x-www-form-urlencoded file_get_contents():假设应用程序/x-www-form-urlencoded 未指定内容类型 - file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded HttpMediaTypeNotSupportedException:不支持内容类型 'application/x-www-form-urlencoded;charset=UTF-8' - HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported 使用 javascript 解析“application/x-www-form-urlencoded”响应 - parse “application/x-www-form-urlencoded” response with javascript Ajax请求:application / x-www-form-urlencoded参数 - Ajax request : application/x-www-form-urlencoded params ionic和php之间的application / x-www-form-urlencoded - application/x-www-form-urlencoded between ionic and php 如何使用应用程序 / x-www-form-urlencoded 发送 axios 帖子? - How to send axios post with application / x-www-form-urlencoded? 带有application / x-www-form-urlencoded的navigator.sendBeacon - navigator.sendBeacon with application/x-www-form-urlencoded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM