0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

axiosでpostした結果をshift-jisで返してるつもりなのに、utf-8で受け取る不思議

Posted at

経緯

getでCSVを取得していたのだが、リクエスト値に追加する文字列が長くなりすぎてエラーが発生。(429 Too Many Requestsエラ)

postに変更することで、既存処理にほぼ手を入れずにCSVを取得できないか試していた。

元々、shift-jisで返却していたのでshift-jisで返却されるだろうと思っていたら、utf-8で返却されCSVが文字化けしてしまった。

対応

ヘッダーのcontent-typeなどを追加したけど上手くいかなかった。
最終的に、axiosをcreateする際に、responseEncodingを指定することで解決。

もちろん、API側のレスポンスもshift_jisで返却している状態

let request = axios.create({
        responseType: 'blob',
        responseEncoding: 'shift_jis',
    })
request.post(url, {
                customer: JSON.stringify(params)
            }).then(response => {
               // 処理
})

その他

Utf-8 BOM付きで返却も考えたが、以下問題もあったため、shift-jisのままとして
・今まで、shift-jisで返却していたためユーザーに変更の通知を行わないと行けなくなってしまう。
・Utf-8 BOMをslackで共有すると、なぜかBOMが外れる

参考URL

Slackのファイル共有を利用するとBOMが消える

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?