【问题标题】:Axios request from express app missing arguments来自 express 应用程序的 Axios 请求缺少参数
【发布时间】:2018-02-05 01:26:20
【问题描述】:

我正在尝试向 FacePlusPlus API 发出 POST 请求。 目前我只是用 node 运行 app.js,但我不断收到错误。

我在 express 应用中的 axios 请求:

axios({
  method: "post",
  url: "https://api-us.faceplusplus.com/facepp/v3/face/analyze",
  headers: {
    'Content-Type': "application/json",
  },
  data: {
    'api_key': '<My api key goes here>',
    'api_secret': '<My api secret goes here>',
    'image_url': 'https://images.pexels.com/photos/220453/pexels-photo-220453.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb'
  }
})
  .then(res => console.log(res))
  .catch(err => console.log(err))

响应错误:

{ Error: Request failed with status code 400
    at createError (/Users/Matt/webdev/test/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/Users/Matt/webdev/test/node_modules/axios/lib/core/settle.js:18:12)
    at IncomingMessage.handleStreamEnd (/Users/Matt/webdev/test/node_modules/axios/lib/adapters/http.js:192:11)
    at emitNone (events.js:110:20)
    at IncomingMessage.emit (events.js:207:7)
    at endReadableNT (_stream_readable.js:1047:12)
    at _combinedTickCallback (internal/process/next_tick.js:102:11)
    at process._tickCallback (internal/process/next_tick.js:161:9)
  config:
   { adapter: [Function: httpAdapter],
     transformRequest: { '0': [Function: transformRequest] },
     transformResponse: { '0': [Function: transformResponse] },
     timeout: 0,
     xsrfCookieName: 'XSRF-TOKEN',
     xsrfHeaderName: 'X-XSRF-TOKEN',
     maxContentLength: -1,
     validateStatus: [Function: validateStatus],
     headers:
      { Accept: 'application/json, text/plain, */*',
        'Content-Type': 'application/json',
        'User-Agent': 'axios/0.17.1',
        'Content-Length': 212 },
     method: 'post',
     url: 'https://api-us.faceplusplus.com/facepp/v3/face/analyze',
     data: '{"api_key":"*** i removed this for question ***","api_secret":"*** i removed this for question ***","image_url":"https://images.pexels.com/photos/220453/pexels-photo-220453.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb"}' }

 ...
 ...


  data: { error_message: 'MISSING_ARGUMENTS: api_key' } } }

数据明明是getting,send,但是为什么会说missing argument呢?

【问题讨论】:

    标签: node.js express http-post axios


    【解决方案1】:

    尝试控制台通过响应记录 api-key,看看你得到了什么,因为它告诉你这是一个缺失的参数。你的数据是一个字符串而不是一个对象,应该是这样的,只是出于好奇?

    【讨论】:

    • API 服务器甚至无法读取请求正文,因为结果证明 api 不接受 application/json。当我在查询字符串中发送表单数据时工作正常。不过感谢您的帮助!
    猜你喜欢
    • 2012-08-26
    • 1970-01-01
    • 2021-09-24
    • 2021-04-15
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多