powershell

Powershellで curlをたたいた結果を見やすく

$response = curl -s https://api.example.com/data
$jsonObject = $response | ConvertFrom-Json
$formattedJson = $jsonObject | ConvertTo-Json -Depth 100 -Compress
$formattedJson
$response = curl -s https://api.example.com/data
$jsonObject = $response | ConvertFrom-Json
$formattedJson = $jsonObject | ConvertTo-Json -Depth 100
$formattedJson
(curl -s https://api.example.com/data | ConvertFrom-Json | ConvertTo-Json -Depth 100)
(curl -s https://api.example.com/data | ConvertFrom-Json | ConvertTo-Json -Depth 100) | Out-File -FilePath formatted.json -Encoding utf8
$response = curl -s https://api.example.com/data
$jsonObject = $response | ConvertFrom-Json
$formattedJson = $jsonObject | ConvertTo-Json -Depth 100
$formattedJson | Out-File -FilePath formatted.json -Encoding utf8
$url = "http://192.168.3.10:8080/testjpg"
$queryParams = "tatecode=012010&yokocode=1020"
$filePath = "D:\testimage\test.jpg"

# フルURLを構築
$fullUrl = "$url?$queryParams"

# PowerShell 7 で curl コマンドを実行
curl -X POST "$fullUrl" -F "file=@$filePath"

スクリプトの詳細

  1. URLの設定: $url にベースURLを設定します。
  2. クエリパラメータの設定: $queryParams にクエリパラメータを設定します。
  3. ファイルパスの設定: $filePath に画像ファイルのパスを設定します。
  4. フルURLの構築: $fullUrl にベースURLとクエリパラメータを結合して設定します。
  5. curlコマンドの実行: PowerShell 7 で curl コマンドを実行して、クエリパラメータとファイルをPOSTします。

このスクリプトをPowerShell 7で実行すると、指定されたURLに対してクエリパラメータと画像ファイルを含むPOSTリクエストが送信されます。

$url = "http://192.168.3.10:8080/testjpg"
$filePath = "D:\\testimage\\test.jpg"

# curl コマンドを実行
curl -X POST "$url" `
     --data-urlencode "tatecode=012010" `
     --data-urlencode "yokocode=1020" `
     --data-urlencode "shisetsu=体育館" `
     -F "file=@$filePath"
# URL エンコードのために .NET の System.Web.HttpUtility クラスを使用
Add-Type -AssemblyName System.Web

$url = "http://192.168.3.10:8080/testjpg"
$queryParams = @{
    tatecode = "012010"
    yokocode = "1020"
    shisetsu = "体育館"
}

# クエリパラメータをエンコードしてリストに格納
$encodedParams = $queryParams.GetEnumerator() | ForEach-Object {
    $key = [System.Web.HttpUtility]::UrlEncode($_.Key)
    $value = [System.Web.HttpUtility]::UrlEncode($_.Value)
    "$key=$value"
}

# リストを & で結合して完全な URL を構築
$encodedParamsString = $encodedParams -join '&'
$fullUrl = "$url?$encodedParamsString"

# 結果を表示
Write-Output "Full URL: $fullUrl"

# ファイルパスを指定(例:)
$filePath = "D:\\testimage\\test.jpg"

# curl コマンドを実行
curl -X POST "$fullUrl" -F "file=@$filePath"

$env:JAVA_TOOL_OPTIONS="-Dfile.encoding=Shift_JIS"
sql hr/hr@localhost:1521/orcl @C:\path\to\test.sql
スポンサーリンク
タイトルとURLをコピーしました