Search code examples
javascriptazure-cognitive-services

Get 415 error while calling computer vision OCR


I am using Azure computer vision API. I can post an image url to Azure and get its result successfully by nodejs.

However when I want to post a local image instead of an image url , I always get 415 error. So did I miss something?

Thanks in advance .


Solution

  • If you want to post a local image to Azure computer version OCR using nodejs. just try the code below :

    const fetch = require("node-fetch")
    const fs  = require('fs');
    
    
    let subscriptionKey = '<your subscription key>';
    let endpoint = '<your computer vision endpoint>';
    let filePath = "<path of your local image>";
    const base64 =  fs.readFileSync(filePath, 'base64')
    const data = Buffer.from(base64, 'base64')
    
    var uriBase = endpoint + "vision/v2.1/ocr";
    
    fetch(uriBase ,
    {
    method: 'POST',
    headers: 
        {
        'Content-Type': 'application/octet-stream',
        'Ocp-Apim-Subscription-Key': subscriptionKey,
        },
    body: data,
    }).then((response) => response.json()).then((data) =>
    {
    console.log(JSON.stringify(data, null, 2));
    }).catch((error) =>
    {
    console.log(error);
    });
    

    As far as I know if you use multipart/form-data, will cause 415 error .

    Result :

    enter image description here