笔记笔记
  • Home
  • AI&ML
  • Example
  • Zoo
  • 关于
⌘ K
PowerShell
最后更新时间:
Copyright © 2023-2024 | Powered by dumi | GuoDapeng | 冀ICP备20004032号-1 | 冀公网安备 冀公网安备 13024002000293号

TABLE OF CONTENTS

‌
‌
‌
‌

PowerShell

发送 http 请求

发送 form-data 请求,好像 PowerShell 5.1 不支持提交二进制数据,所以只能用 Base64。

$apiUrl = "http://192.168.2.200:8002/file/upload"
$filePath = "C:\Users\GDP\Pictures\Screenshots\1.png"
$fileBytes = Get-Content -Path $filePath -Encoding Byte -ReadCount 0
# 构造body并指定boundary
$boundary = [System.Guid]::NewGuid().ToString()
$formattedBody = @"
--{0}
Content-Disposition: form-data; name="element-name"
Content-Type: text/plain
Name fdafda
--{0}
Content-Disposition: form-data; name="image"; filename="{1}"
Content-Type: image/png
{2}
--{0}--
"@ -f $boundary, (Split-Path -Leaf $filePath), [System.Convert]::ToBase64String($fileBytes)
$headers = @{
Accept = "application/json"
"Content-Type" = "multipart/form-data; boundary=$boundary"
}
$fileBytes.Length
$formattedBody.Length
$response = Invoke-RestMethod -Uri $apiUrl -Method Post -Body $formattedBody -Headers $headers
$response

能发送数据,但是我没研究如何接收。

$apiUrl = "http://192.168.2.200:8002/file/upload"
$filePath = "C:\Users\GDP\Pictures\Screenshots\1.png"
Invoke-RestMethod -Uri $apiUrl -Method Post -InFile $filePath -ContentType "multipart/form-data; boundary=WebAppBoundary"