概要
- Node.js + Expressで開発しているWebアプリケーション
- 外部サービスによるSHIFT_JIS形式のPOSTリクエストデータを処理したい
- リクエストボディの取得にはbody-parserモジュールを使用している
Node.jsでは基本的にUTF-8のみなのは知っていましたが、開発に入る時には外部サービスの仕様が明らかでなかったので、この状況に陥っています...。
蓋を開けてみらたら...という感じです????
body-parserがSHIT_JISに未対応なことがエラーの原因になっているのでしょうか。
別の部分のプログラムはほとんど完成してしまったので、なるべく仕様を変えずに進めたいと考えています。
方法があれば、ご教授お願いします。。。
困っていること
- SHIFT_JIS形式のPOSTをされるとサーバーエラーになる
- UTF-8でPOSTすると正常に処理できます
エラーの内容
UnsupportedMediaTypeError unsupported charset "SHIFT_JIS" UnsupportedMediaTypeError: unsupported charset "SHIFT_JIS" at urlencodedParser (/Users/nob3110/nodejs/1901_fanjunction/node_modules/body-parser/lib/types/urlencoded.js:108:12) at Layer.handle [as handle_request] (/Users/nob3110/nodejs/1901_fanjunction/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/Users/nob3110/nodejs/1901_fanjunction/node_modules/express/lib/router/index.js:317:13) at /Users/nob3110/nodejs/1901_fanjunction/node_modules/express/lib/router/index.js:284:7 at Function.process_params (/Users/nob3110/nodejs/1901_fanjunction/node_modules/express/lib/router/index.js:335:12) at next (/Users/nob3110/nodejs/1901_fanjunction/node_modules/express/lib/router/index.js:275:10) at session (/Users/nob3110/nodejs/1901_fanjunction/node_modules/express-session/index.js:454:7) at Layer.handle [as handle_request] (/Users/nob3110/nodejs/1901_fanjunction/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/Users/nob3110/nodejs/1901_fanjunction/node_modules/express/lib/router/index.js:317:13) at /Users/nob3110/nodejs/1901_fanjunction/node_modules/express/lib/router/index.js:284:7</td>
すでに試したこと => いずれも上記エラーになります
- iconvを使った変換処理
//モジュール読み込み const iconv = require("iconv-lite"); //POSTされた時の処理 router.post("/", (req, res) => { const data = iconv.encode(req.body, "UTF-8"); res.render("./index.ejs", data); });
//モジュール読み込み const bodyParser = require("body-parser"); const fs=require("fs"); app.use(bodyParser.urlencoded({ extended: true })); eval(fs.readFileSync("./ecl_new.js",{encoding:"utf8"})); //POSTされた時の処理 router.post("/", (req, res) => { const data = UnescapeSJIS(req.body); res.render("./index.ejs", data); });
- encoding-japaneseモジュールを使った方法
参考: [Node.js] Shift_JISなレスポンスデータをUnicodeに変換する簡単な方法
//モジュール読み込み const encoding = require("encoding-japanese"); //POSTされた時の処理 router.post("/", (req, res) => { const unicodeArr = encoding.convert(req.body, { from: 'SJIS', to: 'UNICODE' }); //UNICODE文字列に変換 // strに文字列として格納 const str = encoding.codeToString(unicodeArr); res.render("./index.ejs", str); });
リクエストのRawデータ
POST /***/*** HTTP/1.1 Content-Type: application/x-www-form-urlencoded; charset=Shift_JIS User-Agent: Jakarta Commons-HttpClient/3.0 Host: 65324832.ngrok.io Content-Length: 633 X-Forwarded-For: 61.215.213.47 terminal_type=0&sps_cust_no=&sps_payment_no=&res_pay_method=docomo&res_result=OK&merchant_id=****&res_err_code=&res_payinfo_key=&free2=&free3=&service_id=001&free1=&camp_type=0&pay_type=1&pay_item_id=&tracking_id=&res_sps_cust_no=&amount=540&item_id=a169574565ee96&res_payment_date=20190312215200&item_name=&tax=&pay_method=docomo%2Cauone&res_date=20190312215200&cust_code=u168e552e2b4398&div_settele=0&limit_second=600&service_type=0&res_sps_payment_no=&last_charge_month=&request_date=20190312215136&auto_charge_type=1&order_id=20190312215136-17&sps_hashcode=1CB01B3229C0ABD6EB8B604B9B74533678211B37&res_tracking_id=50657276214974

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/12 17:27