LoginSignup
1
0

More than 1 year has passed since last update.

curlコマンドの代わりにPowerShellを使う

Last updated at Posted at 2022-08-19

お客様のWindowsにて特定サービスのAPI操作が必要になりました。
いつもならPostmanを入れれば楽なのですが、今回はそういう訳にもいかないためPowerShellで行いました。その際のメモになります。

BOXのAPIで検証します。
フォルダ情報を取得
GET
https://api.box.com/2.0/folders/:folder_id

curl -i -X GET "https://api.box.com/2.0/folders/0" \ -H "Authorization: Bearer <ACCESS_TOKEN>"

検証用のトークンを発行します。
123.png

先程のcurlをPowerShellで置き換えると下記になります。

$token = “<ACCESS_TOKEN>"

$headers = @{"Authorization" = "Bearer " + $token}

$url="https://api.box.com/2.0/folders/0"

Invoke-RestMethod -Uri $url -Method Get -Headers $headers -ContentType "application/json" | ConvertTo-Json

Jsonの結果を取得していることを確認。
image.png

BodyにJsonを入れたコマンドサンプルは以下になります。

$body = '{"aaa": "bbb"}'

Invoke-RestMethod -Uri $url -Method Get -Headers $headers -Body $body -ContentType "application/json" | ConvertTo-Json

以下エラーが発生することがあります。

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.

デフォルトでTLS1.0で設定されていますが、WebにはTLS1.2が要求されるためエラー発生します。解消するにはTLS1.2のプロトコルを有効化してから、コマンドを実行します。

[Net.ServicePointManager]::SecurityProtocol = "Tls, Tls11, Tls12, Ssl3"

以上となります。
見て頂きありがとうございました。

1
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
1
0