【问题标题】:Giving error with node/fastify Unsupported Media Type: application/x-www-form-urlencodednode/fastify 不支持的媒体类型出现错误:application/x-www-form-urlencoded
【发布时间】:2021-02-25 06:19:34
【问题描述】:

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":"不支持 媒体类型","消息":"不支持的媒体类型: 应用程序/x-www-form-urlencoded"}

我觉得和 fastify 有关系真的不知道,因为这是我第一个使用 fastify 的项目。任何帮助都会被应用

【问题讨论】:

  • 似乎不支持媒体类型application/x-www-form-urlencoded
  • 我也试过没有它,但同样的错误
  • 试过没有什么?这没有意义

标签: javascript node.js paytm fastify fastify-multipart


【解决方案1】:

Fastify,开箱即用,仅解析 application/json 有效负载。

要管理表单,您需要将这些插件添加到您的项目中

【讨论】:

    猜你喜欢
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 2022-01-19
    • 2013-03-24
    • 2016-02-21
    • 2020-04-13
    • 2019-05-24
    相关资源
    最近更新 更多