0

Working code for URL Image moderation. It's not working for local images. I get a "remote server return error (400) Bad Request" I am not sure if I should convert the images to bytes. Any help is appreciated.

# 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


premo103
  • 3
  • 2
  • There is nothing in the [docs](https://sightengine.com/docs/getstarted) that implies base-64 encoding. Instead, the file should be included in binary form, as a multipart form-data field. With PowerShell 7+ you can use the `Invoke-RestMethod` parameter `-Form`. See [example](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7.3#example-4-simplified-multipart-form-data-submission). For older PS versions using `curl` instead is propably the easiest way. – zett42 Jan 30 '23 at 21:32
  • Every server is different. You should try to get documentation on your server. You are making a HTTP request while is HTML (text). Any data in the body of a Request must be text. So you have to encode as a base 64 string if the picture is suppose to go in the body. If the image is suppose to go as an attachment then the attachment can be binary. Attachments are MIME and each attachment has a content type. – jdweng Jan 30 '23 at 23:18

0 Answers0