FacePlusPlus,"error_message":"MISSING_ARGUMENTS:api_key",带有React Native提取请求 [英] FacePlusPlus, "error_message": "MISSING_ARGUMENTS: api_key", with React Native fetch request
问题描述
我对本机作出反应还很陌生,我正在尝试使用FacePlusPlus API进行测试( https://console.faceplusplus.com/documents/5679127 ).
I'm fairly new to react native and I'm trying to test out using the FacePlusPlus API (https://console.faceplusplus.com/documents/5679127).
在这里,我尝试将'api_key'放在正文中,但是,我也尝试将其放在标头中.两者都没有.
Here, I've tried putting 'api_key' in the body, however, I've also tried putting it in headers too. Neither has worked.
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)放在其中,数据是一个数组,用于查看响应,但是我得到的只是
In render(), I put console.log(this.state.data) where data is an array to see the response, however all I keep getting is
Object {
"error_message": "MISSING_ARGUMENTS: api_key",
}
推荐答案
要解决此问题,您必须将Content-Type标头设置为'application/x-www-form-urlencoded' 并将您的参数作为formData传递. 我以使用'request'npm包为例.
To solve this problem you have to set Content-Type header to 'application/x-www-form-urlencoded' and pass your arguments as formData. I put the example with using 'request' npm package.
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));
});
这篇关于FacePlusPlus,"error_message":"MISSING_ARGUMENTS:api_key",带有React Native提取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!