How send image in base64 in azure cognitive services face

Maj PMESP FERNANDES 20 Reputation points
2023-08-29T01:13:38.21+00:00

Hi everyone. How i can send a image in base 64?

I trying send using application/octet-stream in the header and the body with :

"/9j/4a......"

Then i received the error"

{
    "error": {
        "code": "BadArgument",
        "message": "Invalid Media Type."
    }
}

Azure Face
Azure Face
An Azure service that provides artificial intelligence algorithms that detect, recognize, and analyze human faces in images.
155 questions
0 comments No comments
{count} votes

Accepted answer
  1. RevelinoB 2,780 Reputation points
    2023-08-29T04:04:37.1333333+00:00

    Hi Maj,

    When I look at your post. It looks like you're trying to send an image in base64 format to some API or service, but you're encountering an "Invalid Media Type" error. Let's break down the steps to send an image in base64 format correctly.

    1. Base64 Encoding: First, make sure you've properly encoded the image into base64. The string you provided, starting with "/9j/4a...", seems to be a valid base64-encoded image, but ensure that it's complete and hasn't been truncated.
    2. Request Header: For sending an image in base64 format, you typically need to set the Content-Type header to indicate that you're sending binary data. Using application/octet-stream is a good choice. So, your request header should look something like:

    Content-Type: application/octet-stream

    1. Request Body: The base64-encoded image should be included in the request body. Make sure you've correctly structured the request body to include the image data. If you're sending JSON as the request body, the image data should be properly escaped. Your JSON might look like:
    {
        "image": "/9j/4a..."
    }
    
    
    

    If you're sending the base64-encoded image as raw data, ensure that you're sending it correctly without any additional JSON wrapping.

    API Endpoint: Verify that you're sending the request to the correct API endpoint with the appropriate HTTP method (e.g., POST or PUT) that the API expects for uploading images.

    Content Length: Make sure you set the Content-Length header to the length of the data you're sending in the request body.

    Here's an example using Python's requests library to send a base64-encoded image to an API:

    import requests
    
    image_data = "/9j/4a..."  # Your complete base64-encoded image data
    url = "https://example.com/upload"  # Replace with the actual API endpoint
    
    headers = {
        "Content-Type": "application/octet-stream",
        "Content-Length": str(len(image_data))
    }
    
    response = requests.post(url, headers=headers, data=image_data)
    
    print(response.status_code)
    print(response.json())  # If the response is expected to be in JSON
    

    If you've followed these steps and are still encountering the "Invalid Media Type" error, there might be an issue with the API or service you're interacting with. Double-check the API documentation and make sure you're using the correct headers and formatting for your request. I hope this helps with your query.

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful