You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell向VirusTotal发送POST请求格式错误:文件参数缺失

解决VirusTotal API文件上传400错误问题

你遇到的400 Bad Request: argument file is missing错误,核心原因是你将文件打包成了data URI格式传递,但VirusTotal的/api/v3/files接口要求直接通过multipart/form-data上传二进制文件,而非base64编码的data字符串。以下是两种可行的修复方案:

方案1:使用PowerShell内置的-Form参数(推荐)

PowerShell的Invoke-WebRequest提供了-Form参数,会自动处理multipart/form-data的边界、文件编码和请求头格式,无需手动构建复杂的请求体:

$apiKey = "MY_API_KEY"
$filePath = "C:\path\to\your\target-file"

$headers = @{
    "accept" = "application/json"
    "x-apikey" = $apiKey
}

# 直接通过-Form参数上传文件
$response = Invoke-WebRequest -Uri "https://www.virustotal.com/api/v3/files" -Method POST -Headers $headers -Form @{
    file = Get-Item $filePath
}

# 解析并输出响应结果
$response.Content | ConvertFrom-Json

方案2:手动构建正确的multipart/form-data请求体

如果需要手动控制请求体细节,需注意以下几点:

  • 边界字符串需与请求头中的boundary值完全一致
  • 需为file字段指定filename和对应文件的MIME类型
  • 直接传递文件的二进制内容,而非base64编码的data URI

代码示例:

$apiKey = "MY_API_KEY"
$filePath = "C:\path\to\your\target-file"
$fileName = Split-Path -Leaf $filePath
$fileBytes = [System.IO.File]::ReadAllBytes($filePath)
$mime = Get-MimeType -Extension ([System.IO.Path]::GetExtension($filePath).TrimStart('.'))

# 定义边界字符串(避免使用特殊字符)
$boundary = "011000010111000001101001"
$headers = @{
    "accept" = "application/json"
    "x-apikey" = $apiKey
    "Content-Type" = "multipart/form-data; boundary=$boundary"
}

# 构建请求体头部
$bodyPrefix = @"
--$boundary
Content-Disposition: form-data; name="file"; filename="$fileName"
Content-Type: $mime

"@

# 构建请求体尾部
$bodySuffix = @"
--$boundary--
"@

# 拼接完整请求体(字符串转字节+文件二进制+尾部字节)
$prefixBytes = [System.Text.Encoding]::UTF8.GetBytes($bodyPrefix)
$suffixBytes = [System.Text.Encoding]::UTF8.GetBytes($bodySuffix)
$fullBody = $prefixBytes + $fileBytes + $suffixBytes

# 发送请求
$response = Invoke-WebRequest -Uri "https://www.virustotal.com/api/v3/files" -Method POST -Headers $headers -Body $fullBody

# 解析响应
$response.Content | ConvertFrom-Json

内容的提问来源于stack exchange,提问作者user24056377

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 14:22:49