[英]http POST request response with “status”: 200 but with an empty object
我正在尝试将API与HTTP POST请求一起使用,并且似乎可以正常工作,但是当我收到响应时,它为空。
API应该在体内接收base64
编码的图像。
来自于promise的promise的参数imageToRead
是从react-native-camera
的Camera
组件返回的。 我不确定可能是那里的问题吗?
这是内容: imageToRead = "/var/mobile/Containers/Data/Application/01B81FC9-B171-11GB-9B48-9D06F89B175A/Documents/3A997ACD-D63C-41BD-9041-FDB834A0672A.jpg"
该API还可以接收formData
文件,以及我如何对其进行测试,并且在Node环境中运行良好。 但是因为我的应用程序是本地iOS应用程序,所以我不得不使用其他工具(例如React Native),不能使用相同的工具。
在Node中测试API的代码:
var formData = {image: fs.createReadStream('/Users/Desktop/APITest/img/testOCR8.jpg')};
request.post({
url:'${url}',
formData: formData},
(err, httpResponse, body) => {
if (err) {
return console.error('upload failed:', err);
}
console.log(body);
});
您可以看到我使用fs
模块中的fs.createReadStream
创建文件流,然后在请求正文中使用它。 我无法使用React Native复制同一件事(如果您有解决方案,那么我可以使用React Native做到这一点,那就更好了!)
所以我尝试了不同的方法,并尝试将我从react-native-camera
附带的camera.capture()
方法获得的文件编码为base64
并将其放置在主体中,但是我得到的响应为空,没有任何错误,只是一个空的对象。
用React Native编写代码:
recognise = (imageToRead) => {
RNFetchBlob.fs.readFile(imageToRead , 'base64')
.then((data) => {
fetch('${url}',{
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: data // the body contain the encoded image
})
.then((res) => { // promise returned with the response from the API
console.log(`i am the base64Image: ${data}`)
console.log('i am response', res)
})
.catch((errInFetch) => { // catch any error in the API
console.log('i am error:', errInFetch)
})
})
.catch((err) => {
console.log(err);
})
}
响应:
{ type: 'default',
status: 200,
ok: true,
statusText: undefined,
headers:
{ map:
{ server: [ 'nginx/1.10.3' ],
'content-type': [ 'application/json; charset="utf-8"' ],
'access-control-allow-origin': [ '*' ],
date: [ 'Thu, 10 May 2018 10:17:48 GMT' ],
'access-control-allow-headers': [ 'x-requested-with' ],
'content-encoding': [ 'gzip' ],
'content-length': [ '237' ],
connection: [ 'keep-alive' ] } },
url: 'https://api.openalpr.com/v2/recognize_bytes?secret_key=key&country=eu',
_bodyInit:
{ _data:
{ size: 371,
blobId: '5080ACA4-5D13-469C-B755-96B06A161FC6',
type: 'application/json',
offset: 0,
name: 'recognize_bytes' } },
_bodyBlob:
{ _data:
{ size: 371,
blobId: '5080ACA4-5D13-469C-B755-96B06A161FC6',
type: 'application/json',
offset: 0,
name: 'recognize_bytes' } } }
我希望有人可以提供帮助。 我已经为此苦了很长时间了。
.then((res) => {
console.log(`i am the base64Image: ${data}`)
console.log('i am response', res)
return res.json();
})
.then(res => {
console.log(res);
})
.catch((errInFetch) => { // catch any error in the API
console.log('i am error:', errInFetch)
})
获取响应主体将返回一个Promise,它将由json()解析。
因此,您可以从res.json()
获得真实数据。
fetch
返回一个promise
包含响应(一个Response对象)。 然后,您再次需要使用response.json()
来解析它,该返回返回诺言解析的JSON
。
fetch(url, {
body: JSON.stringify(data),
cache: 'no-cache',
headers: {
'user-agent': 'Mozilla/4.0 MDN Example',
'content-type': 'application/json'
},
method: 'POST', // *GET, POST, PUT, DELETE, etc.
})
.then(response => response.json())
有关更多信息: 请阅读
200状态是成功获取http请求的状态。 您的API应该返回状态201进行发布,并显示一条消息,而不是200,例如:
res.status(201).send(data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.