Node.js/fFastify 出现错误:“不支持的媒体类型:application/x-www-form-urlencoded”

问题描述 投票:0回答:3

考虑:

文件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 error
    }

    // Store the URL and checksum
    let url = data.url;
    let checksum = data.checksum;

    // Delete it from the 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 error
      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":"不支持 媒体类型","message":"不支持的媒体类型: 应用程序/x-www-form-urlencoded"}

我认为这与 Fastify 有关。我真的不知道,因为这是我使用 Fastify 的第一个项目。我该如何解决它?

javascript node.js paytm fastify fastify-multipart
3个回答
20
投票

Fastify,开箱即用,仅解析

application/json
有效负载。

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


1
投票

为了完整起见,我需要除了这两个之外的其他东西(我需要多部分/相关)。您可以在 Fastify 实例上使用“addContentTypeParser”添加您自己的内容类型解析器。

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


1
投票

Fastify 推荐使用 @fastify/formbody 插件来解析 x-www-form-urlencoded 主体。

通过npm安装:

npm i @fastify/formbody

ES6

import fastify from 'fastify'
import formbody from '@fastify/formbody'

const app = fastify({ logger: false });
app.register(formbody)

ES5

const fastify = require('fastify')()
fastify.register(require('@fastify/formbody'))
© www.soinside.com 2019 - 2024. All rights reserved.