解決 cUrl 下載的網頁或 JSON 看起來像是亂碼的問題

cUrl 是使用網路協定傳輸資料的開源工具。使用 cUrl 下載的網頁 或 JSON 看起來像是亂碼的二進制檔案 (Binary file),而不是預期的網頁或 JSON 文字。 原因是啟用了壓縮內容 (accept-encoding) 的選項 ,只要將該下載的檔案解壓縮或者是 cUrl 選項移除壓縮內容選項後重新下載就可以解決問題。

Photo by Matheus Bertelli from StockSnap

問題狀況

curl 指定了壓縮內容的選項 --header 'accept-encoding: gzip, deflate, br'
curl --location 'https://www.googleapis.com/discovery/v1/apis?fields=' \
  --verbose \
  --header 'accept-encoding: gzip, deflate, br' \
  --output save.json

下載的檔案內容原本預期是 JSON 文字,但是打開看到像是亂碼的二進制檔案

問題原因

MDN Web Docs 解釋了什麼是 Accept-Encoding :指定伺服器要傳給客戶端的內容編碼方式,通常是壓縮演算法。
The Accept-Encoding request HTTP header advertises which content encoding, usually a compression algorithm, the client is able to understand. Using content negotiation, the server selects one of the proposals, uses it and informs the client of its choice with the Content-Encoding response header.

「--header 'accept-encoding: gzip, deflate, br' 」告知伺服器可以選擇壓縮內容的三種選項 gzipdeflate (zlib), br (Brotli)。最終伺服器選擇的是 gzip

解決方式

方案1: 將下載的檔案副檔名加上 .gzip 例如上例 save.json 改成 save.json.gzip。解壓縮後得到 save.json 將會是預期的 JSON 文字。

方法2: cUrl 選項移除壓縮內容 (accept-encoding) 選項後重新下載
curl --location 'https://www.googleapis.com/discovery/v1/apis?fields=' \
  --verbose \
  --output save.json
或者是清空壓縮內容 (accept-encoding) 選項
curl --location 'https://www.googleapis.com/discovery/v1/apis?fields=' \
  --verbose \
  --header 'accept-encoding:' \
  --output save.json

打開下載檔案可以看到預期的 JSON 文字



參考資料


圖片素材






留言