简体   繁体   中英

Convert character set from none-standard one to UTF-8

There is a page with <meta charset="EUC-KR"> , say address-search.foo.com that searches an address and sends it to a specified url by submitting html form of method POST like below.

let form = <form element>;
form.zipCode.value = "63563";
form.address.value = "제주특별자치도 서귀포시 이어도로 579 (강정동)";
form.action = "https://my-service.foo.com";
form.method = "post";
form.submit();

And there is a POST handler in my-service.foo.com run by ExpressJs to take the above request like below.

const next = require("next");
const app = next(nextConfig);

const server = express();
server.use(bodyParser.urlencoded({ extended: false }));
server.use(bodyParser.json());

server.post("/", (req, res) => {
  console.log(req.body);

  app.render(req, res, "/");
});

And the console.log(req.body); above prints below.

[Object: null prototype] {
  zipCode: '63563'
  address: '����Ư����ġ�� �������� �̾�� 579 (������)'
}

I tried to convert the encoding of req.body.address using iconv-lite module, but it doesn't work as it does on PHP like below.

iconv("CP949", "UTF-8", $_POST['address']); // working very happily

How to properly use iconv-lite or is there anyway to get around this on ExpressJs?

Use urlencode module.

I solved it by using bodyParser.raw() instead of bodyParser.urlencoded() .

const urlencode = require("urlencode");
const iconv = require("iconv-lite");
const qs = require("querystring");

server.use(
  bodyParser.raw({ type: "application/x-www-form-urlencoded" }),
  (req, res, next) => {
    if (req.method === "POST") {
      const decoded = iconv.decode(req.body, "utf8");
      /**
       * Define another conditional statement that filters an encoding specific case.
       * Below if statement is just for my case.
       */
      if (req.path === "/some/path/to/treat/differently") {
        req.body = urlencode.parse(decoded, { charset: "euc-kr" })
      }
      else {
        req.body = qs.parse(decoded);
      }
    }
    next();
  }
);

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