Curlでhttpsでpost時に安全でないSSL/TLS接続を無視する

  • 作成日 2020.05.16
  • 更新日 2020.07.17
  • 小ネタ
Curlでhttpsでpost時に安全でないSSL/TLS接続を無視する

apiでpostリクエストをcurlコマンドで叩いた際に、API先の環境が信頼された証明機関で発行されていない場合や、有効期限が切れているSSLの場合にデータが取得できないので、その際の対応を記載

環境

OS CentOS Linux release 7.7.1908 (Core)

curl実行時のエラー内容

実行コマンドサンプル

curl -H "Content-Type: application/json" -d '{"username" : "user","password" : "pass"}' -XPOST https://sample.com/sample/api/v1/auth

エラーメッセージ

curl: (6) Could not resolve host: POST; 不明なエラー
curl: (60) Issuer certificate is invalid.
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.

対応方法

言われた通り -k を追加する

curl -H "Content-Type: application/json" -d '{"username" : "user","password" : "pass"}' -XPOST -k https://sample.com/sample/api/v1/auth

jsonを整形して表示する場合は「 | jq 」を付けてあげる

curl -H "Content-Type: application/json" -d '{"username" : "user","password" : "pass"}' -XPOST -k https://sample.com/sample/api/v1/auth | jq