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

URL图片审核代码正常,本地图片调用Sightengine API报400错误求助

问题:本地图片调用Sightengine API返回400 Bad Request错误

现有PowerShell代码可正常实现URL图片审核功能,但处理本地图片时,调用Sightengine Image API返回“远程服务器返回错误(400) Bad Request”。不确定是否需要将图片转换为字节数组,寻求技术帮助。

原代码:

# Import necessary modules
# Set your API user and secret
$api_user = "#####"
$api_secret = "####"

# Set the path to the image you want to moderate
$image_path = "C:\Temp\Pictures\sportsillustrated.png"

$URL = "https://www.micds.org/wp-content/uploads/2018/06/WaterPolo-Varsity-Boys.jpg"

# Read the image into a byte array
$image_bytes = [System.IO.File]::ReadAllBytes($image_path)

# Convert the byte array to a base64 string
$image_base64 = [System.Convert]::ToBase64String($image_bytes)

# Set the API endpoint
$api_endpoint = "https://api.sightengine.com/1.0/check.json"

# Set the parameters for the API call
$params = @{
    "models" = "nudity-2.0"
    "url" = $URL
    #"media" = $image_base64
    'api_user' = $api_user
    'api_secret' = $api_secret

}

# Make the API call
$response = Invoke-RestMethod -Uri $api_endpoint -Method Post -Body $params

# Output the response
$response
解决方案
  • 问题核心:Sightengine API处理本地图片时,要求通过multipart/form-data格式上传文件,而非直接传递Base64字符串。原代码即使启用media参数传Base64,也不符合API的接收规则,同时不能同时传递url和本地文件参数。

  • 修改后的代码:

# 设置API凭证
$api_user = "#####"
$api_secret = "####"

# 本地图片路径
$image_path = "C:\Temp\Pictures\sportsillustrated.png"

# API端点
$api_endpoint = "https://api.sightengine.com/1.0/check.json"

# 构建表单数据:上传本地文件,同时传递API参数
$formParams = @{
    models = "nudity-2.0"
    api_user = $api_user
    api_secret = $api_secret
    media = Get-Item -Path $image_path
}

# 发送API请求,使用-Form参数自动处理multipart/form-data格式
$response = Invoke-RestMethod -Uri $api_endpoint -Method Post -Form $formParams

# 输出响应结果
$response
  • 关键说明:
    • 移除了url参数,确保只传递本地文件
    • 使用-Form参数替代-Body,PowerShell会自动将文件转换为符合要求的上传格式
    • 直接通过Get-Item传递文件对象,无需手动处理字节数组或Base64转换

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 01:05:26