ajax formdata 请求发生问题
发布于 5 年前 作者 xumjs8623 3588 次浏览 来自 问答

实现的需求是,将人脸图片base64送到face++旷世 人脸检测api那边进行检测,然后返回人脸相关数据; 在postman中调用一起正常,但是我把请求代码一模一样拷贝到node(egg框架)中就一直提示缺少参数 api_key 旷世那边的接口只能用multipart/form-data 不支持json访问 postman中截图: image.png

var request = require("request");

var options = { method: 'POST',
  url: 'https://api-cn.faceplusplus.com/facepp/v3/detect',
  headers: 
   { 'cache-control': 'no-cache',
     Connection: 'keep-alive',
     'content-length': '748',
     'accept-encoding': 'gzip, deflate',
     Host: 'api-cn.faceplusplus.com',
     'Postman-Token': '6d225b52-848b-460a-ab8a-11bd9c635fae,6d7e22ef-c841-41f7-b6c0-53d1f501252b',
     'Cache-Control': 'no-cache',
     Accept: '*/*',
     'User-Agent': 'PostmanRuntime/7.13.0',
     'Content-Type': 'application/x-www-form-urlencoded',
     'content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' },
  formData: 
   { api_key: 'ZvPbcqfQ1o1jHDpn-ok_qOqPT-f6NLIS',
     api_secret: '****************',
     image_url: 'https://public-mj.oss-cn-shanghai.aliyuncs.com/%E8%A8%80logo.png',
     return_landmark: '1',
     return_attributes: 'gender,age' } };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

以上代码返回 image.png

请问各位大佬有没有遇到过这个情况,实在是没办法了

4 回复

跟 egg 没啥关系,你用的是 request 这个库,去看它文档吧,看提示就是传错了。另外,为啥用 callback 的方式。。。 而且为啥不直接用 egg 自带的 curl

@atian25 我原来用的是axios, 因为一直提示缺少参数,我就在postman 中把请求代码一模一样拷过来了测试 image.png

我去用curl再写一个试试,再去研究研究文档,谢谢哈

这种提示都很明显了。。。 自己抓个包看看送了啥不就知道了。

@atian25 解决了 谢谢哈,旷世那边的技术顾问跟我讲 只能用multipart/form-data, 昨天一直不行,今天自己试了一下application/x-www-form-urlencoded 就可以了

回到顶部