尝试找出使用Node.js Needle library向API发送RESTful请求的正确方式。我认为除了关于图像URL的代码之外,一切都是正确的。无论我如何尝试更改它的外观或放置位置,我总是收到一个错误,指出它是一个无效的图像,但事实并非如此,URL是正常的。因此,我猜我的代码是错误的,所以无论它认为是图像的URL,都可能不是URL (而可能是其他代码,或者应该是主体/图像URL所在位置的代码)。
const imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/37/Dagestani_man_and_woman.jpg'
// Request parameters.
const params = {
returnFaceId: true,
returnFaceLandmarks: false,
returnFaceAttributes: 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'
}
var options = {
body: '{"url": ' + '"' + imageUrl + '"}',
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': subscriptionKey
}
}
needle.post(endpoint, params, options, function (err, res, body) {
console.log(`Status: ${res.statusCode}`)
console.log('Body: ', body)
console.log('ERROR: ' + err)
//console.log(res)
})我还试着像写一个简单的老对象:body = { 'url': imageURL}一样编写正文,但仍然得到相同的错误。
错误:
Status: 400
Body: { error: { code: 'InvalidURL', message: 'Invalid image URL.' } }下面是我正在尝试调用的接口,已经确认可以与其他示例一起使用:https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236
发布于 2020-05-29 20:19:12
对于此请求,您可以使用以下参数组合:
将它们中的一些作为查询字符串(您的‘’)作为主体有效负载(您的options.body)进行
因此,您似乎不能直接使用needle.post,因为它可以执行查询字符串参数或主体参数,但不能同时执行这两者。
因此,有几个选项:
Change
对于第一个选项,下面是一个示例:
const imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/37/Dagestani_man_and_woman.jpg'
// Request parameters.
const params = {
returnFaceId: true,
returnFaceLandmarks: false,
returnFaceAttributes: 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'
}
// Adding params to query string
serialize = function(obj) {
var str = [];
for (var p in obj)
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
}
endpoint = endpoint + "?" + serialize(params)
// Setting body and options
var body = '{ "url": ' + '"' + imageUrl + '"}'
var options = {
headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': subscriptionKey
}
}
needle.post(endpoint, body, options, function (err, res, body) {
console.log(`Status: ${res.statusCode}`)
console.log('Body: ', body)
console.log('ERROR: ' + err)
//console.log(res)
})https://stackoverflow.com/questions/61947188
复制相似问题