Can't send base64 encoded image with octet stream

OMER FARUK DOGANCAY 1 Reputation point
2022-07-13T18:46:55.637+00:00

Hi,

I am trying to send a base64 encoded image to vision API for image description. I could send images files from public storage with no problem by passing their URL as described in the documentation but I can't send images by their base64 encoded data.

async function handleAIRequest(params) {
const postID = params.postID;
const imageBytes = params.imageBytes;

var body = imageBytes;  

const requestURL = `https://[our subscription].cognitiveservices.azure.com/vision/v3.2/describe?maxCandidates=1&language=en&model-version=latest`;  
const requestPayload = {  
    method: 'POST',  
    headers: {  
        'Content-Type': 'application/octet-stream',  
        'processData': false,  
        'Ocp-Apim-Subscription-Key': process.env.AZURE_KEY,  
    },  
    body: makeblob2(imageBytes)  
}  

// console.log("SEND", requestURL, requestPayload)  

const rawResponse = await fetch(requestURL, requestPayload);  
const result = await rawResponse.json();  

console.log(rawResponse, result);  

}

I am getting this error

{
"error": {
"code": "InvalidRequest",
"innererror": {
"code": "InvalidImageFormat",
"message": "Input data is not a valid image."
},
"message": "Input data is not a valid image."
}
}4p4UFf5flwfj8dH2+TLNjMixLlE8J3kJf73AEgWt6UGitzxAAAAAElFTkSuQmCC

Computer Vision
Computer Vision
An Azure artificial intelligence service that analyzes content in images and video.
415 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. romungi-MSFT 48,906 Reputation points Microsoft Employee Moderator
    2022-07-14T11:24:52.557+00:00

    @OMER FARUK DOGANCAY I think you are trying this scenario with JS, I do not have much experience using JS but I see that you are assigning the binary data to the body variable body = imageBytes and also using a call to makeblob2(imageBytes) to pass the actual binary data with the body in the payload. I think in this case body = imageBytes is not necessary.

    Although, this is an older thread from SO it is a similar request to pass the image with base64 encoding for reference.

    Also, have you tried to use the JS client libraries to pass the local image? computerVisionClient.describeImageInStream() should be straightforward and the client could handle the image format if you just pass the image path.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.