简体   繁体   中英

Giving error with node/fastify Unsupported Media Type: 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 is working fine but when it redirects to /true route it giving error

{"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. Any help will be appricated

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

{"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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM