【问题标题】:FacePlusPlus, "error_message": "MISSING_ARGUMENTS: api_key", with React Native fetch requestFacePlusPlus,“error_message”:“MISSING_ARGUMENTS:api_key”,带有 React Native 获取请求
【发布时间】:2018-02-06 21:31:56
【问题描述】:

我对本机反应还很陌生,我正在尝试使用 FacePlusPlus API (https://console.faceplusplus.com/documents/5679127) 进行测试。

在这里,我尝试将 'api_key' 放入正文中,但是,我也尝试将其放入标题中。两者都没有奏效。

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",
} 

【问题讨论】:

    标签: react-native fetch api-key


    【解决方案1】:

    要解决此问题,您必须将 Content-Type 标头设置为 'application/x-www-form-urlencoded' 并将您的参数作为 formData 传递。 我举了一个使用'request' 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));
     });
    

    【讨论】:

    • 谢谢,这对我来说是个问题。他们没有很好的文档
    猜你喜欢
    • 2018-12-24
    • 2018-05-08
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2021-04-25
    • 1970-01-01
    相关资源
    最近更新 更多