簡體   English   中英

具有“狀態”的http POST請求響應:200,但對象為空

[英]http POST request response with “status”: 200 but with an empty object

我正在嘗試將API與HTTP POST請求一起使用,並且似乎可以正常工作,但是當我收到響應時,它為空。

API應該在體內接收base64編碼的圖像。

來自於promise的promise的參數imageToRead是從react-native-cameraCamera組件返回的。 我不確定可能是那里的問題嗎?

這是內容: 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);

完整的例子

另外,您可以查看狀態201說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM