调用计算机视觉OCR时出现415错误
Get 415 error while calling computer vision OCR
我正在使用 Azure 计算机视觉 API。我可以 post 一个图像 url 到 Azure 并通过 nodejs 成功获得它的结果。
然而,当我想要 post 本地图像而不是图像 url 时,我总是会收到 415 错误。那我错过了什么吗?
提前致谢。
如果你想post使用nodejs将本地图片到Azure电脑版OCR。试试下面的代码:
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);
});
据我所知,如果你使用multipart/form-data
,会导致415错误。
结果:
我正在使用 Azure 计算机视觉 API。我可以 post 一个图像 url 到 Azure 并通过 nodejs 成功获得它的结果。
然而,当我想要 post 本地图像而不是图像 url 时,我总是会收到 415 错误。那我错过了什么吗?
提前致谢。
如果你想post使用nodejs将本地图片到Azure电脑版OCR。试试下面的代码:
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);
});
据我所知,如果你使用multipart/form-data
,会导致415错误。
结果: