【问题标题】:Send a POST request with an image URL? --- Needle library发送带有图像 URL 的 POST 请求? --- 针库
【发布时间】:2020-09-08 19:57:47
【问题描述】:

试图找出使用 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)
})

我也尝试将正文写成普通的 ol' 对象:body = { 'url': imageURL},但仍然遇到相同的错误。

错误:

Status: 400
Body:  { error: { code: 'InvalidURL', message: 'Invalid image URL.' } }

这是我尝试调用的 API,已确认可与其他示例一起使用: https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236

【问题讨论】:

    标签: node.js rest microsoft-cognitive needle.js


    【解决方案1】:

    对于这个请求,你有一个参数组合:

    • 其中一些作为查询字符串(您的“参数”)
    • 其中一些作为正文有效负载(您的 options.body)

    因此,您似乎不能直接使用needle.post,因为它可以查询字符串参数或正文参数,但不能同时进行。

    所以有几种选择:

    • 在 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'
    }
    
    // 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)
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 2018-12-14
      • 2011-06-10
      相关资源
      最近更新 更多