react-native - FacePlusPlus, "error_message": "MISSING_ARGUMENTS: api_key", 与 React Native 获取请求

标签 react-native fetch api-key

我对原生 react 相当陌生,我正在尝试使用 FacePlusPlus API ( https://console.faceplusplus.com/documents/5679127 ) 进行测试。

在这里,我尝试将“api_key”放入正文中,但是,我也尝试将其放入 header 中。两者都不起作用。

componentDidMount() {
    var url = 'https://api-us.faceplusplus.com/facepp/v3/detect';

    return fetch(url, {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        api_key: 'blahblahblah',
        api_secret: 'blahblahblah',
      })
    })
    .then((response) => response.json())
    .then((responseJson) => {
      this.setState({
        isLoading: false,
        data: responseJson,
      }, function() {
        // do something with new state
      });
    })
    .catch((error) => {
      console.error(error);
    });
  }

在 render() 中,我将 console.log(this.state.data) 放在 data 是一个数组中以查看响应,但是我得到的只是

Object {
   "error_message": "MISSING_ARGUMENTS: api_key",
} 

最佳答案

要解决此问题,您必须将 Content-Type header 设置为“application/x-www-form-urlencoded” 并将您的参数作为 formData 传递。 我使用“请求”npm 包来放置示例。

 const request = require('request');

 request.post({url:'https://api-us.faceplusplus.com/facepp/v3/compare', formData: {
   api_key: 'your api key',
   api_secret: 'your api secret',
   image_url1: 'https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/George_Lucas_cropped_2009.jpg/220px-George_Lucas_cropped_2009.jpg',
   image_url2: 'https://imgix.bustle.com/uploads/getty/2018/6/13/e4c5921d-3e23-4f13-87fe-0180005d0ace-getty-929360234.jpg?w=970&h=582&fit=crop&crop=faces&auto=format&q=70'
 }}, (err, httpResponse, body) => {
   if (err) {
     return console.error('error', err);
   }
   console.log('success ', JSON.parse(body));
 });

关于react-native - FacePlusPlus, "error_message": "MISSING_ARGUMENTS: api_key", 与 React Native 获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48652293/

相关文章:

php - 我想在 .CSS 中使用 .php 创建一个 "message"之类的窗口,并从 mysql 中获取。每行一个新 block

asp.net-core - IdentityServer4 API key 支持

javascript - React-native get方法返回值

android - React Native 中的 Flexbox 使用的是 dp 还是 pixel?

git - 克隆裸 git 存储库的单个路径

javascript - 如何推迟执行代码,直到获取返回值

azure - 如何将包含 secret API key 的公共(public) Github 项目部署到 Azure?

google-cloud-platform - 使用 Nodejs 在 google 云端点上工作时出现 "Unsupported auth type for "问题尝试此 API“- api_key :apiKey.”

javascript - 如何使用 React JS 迭代在 UI 中作为 this.state.data 访问的 Java hibernate DAO 返回的列表?

android - android 和 ios 中的 https 获取请求阻塞 - React Native