【ERROR 400 Bad Request】対処方法(wget)


はじめに

確認環境

  • Windows11
  • WSL2
  • Ubuntu

状況

  • wgetコマンドでファイルをダウンロード
    • ダウンロードされていない(ローカルにファイルが作成されない)
  • プロンプトにエラーが出力される
    • 「command not found」
  • ログファイル(wget-log)にエラーが出力される
    • 「HTTP request sent, awaiting response… 400 Bad Request」
    • 「2022-11-15 13:00:23 ERROR 400: Bad Request.」

再現

wget実行

wget https://・・・data.zip
$ wget https://・・・data.zip
[1] 595
[2] 596
[3] 597
[4] 598
[2]   Done                    AWSAccessKeyId=・・・
[3]-  Done                    Signature=・・・
$
Redirecting output to ‘wget-log’.
・・・
: command not found

[1]-  Exit 8                  wget https://・・・
[4]+  Exit 127                ・・・
$ ls
wget-log
  • command not found
    • エラーが出力される
  • wget-log
    • エラー内容が記載されたファイル
    • うまく行かない場合には必ずできる
    • ファイルはダウンロードされず、「wget-log」があるのみ

wget-logに出力されるエラー内容を確認

cat wget-log
$ cat wget-log
--2022-11-15 13:00:23--  https://・・・data.zip
Resolving 接続先ホスト (ドメイン)... xxx.xxx.xxx.xxx, xxx.xxx.xxx.xxx, ...
Connecting to 接続先ホスト (ドメイン)|xxx.xxx.xxx.xxx|:443... connected.
HTTP request sent, awaiting response... 400 Bad Request
2022-11-15 13:00:23 ERROR 400: Bad Request.
  • ERROR 400 Bad Request
    • HTTPプロトコルエラー番号
    • 不正なリクエストの意味
    • 正常の場合は「200」

対処方法

原因

  • URLの文字列に「%」が含まれている

対処

  1. 「"」でURLを囲む
  2. 「-O」でファイル名を指定する
    • エラーの原因ではないので必須ではない
    • 変なファイル名になるので「-O」に続けてファイル名を書いておいた方が分かりやすい

wget -O data.zip "https://・・・data.zip"
$ wget -O data.zip "https://・・・"
--2022-11-15 12:22:16--  https://・・・
Resolving 接続先ホスト (ドメイン)... xxx.xxx.xxx.xxx, ...
Connecting to 接続先ホスト (ドメイン)|xxx.xxx.xxx.xxx|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 620597 (606K) [application/zip]
Saving to: ‘data.zip’

data.zip                            100%[===================================================================>] 606.05K  --.-KB/s    in 0.1s

2022-11-15 12:22:17 (5.84 MB/s) - ‘data.zip’ saved [620597/620597]
  • 「200 OK」
    • 通信が正常に行われた
  • 「data.zip saved」
    • ファイルも保存されたメッセージ

補足「ERROR 403: Forbidden.」の場合

状況

  • wgetの失敗を繰り返すと、サーバ側のセキュリティ要件に抵触する場合がある
  • HTTPプロトコル「403」は”拒否”
  • コマンドが間違っていなくてもダウンロードはできない

対処

  • 基本できない
  • しばらく待ってみる(サーバ側のセキュリティ要件による)


Posted by futa