2012-05-29 23 views
2

我從Node.js進程調用Firebase REST API。我看到的問題是,帖子正文包含非ASCII字符時POSTS失敗。儘管請求返回了「200」狀態,並且節點的名稱(實際上並未創建)。Firebase REST API - 如何設置字符編碼?

我目前想是這樣的:

function push(path, object, callback) { 
    console.log("Pushing to "+path+" on: "+firebase.host); 
    var fullPath=firebase.basePath+path; 
    console.log("fullPath="+fullPath); 
    var body = JSON.stringify(object); 
    var options = { 
     host: firebase.host, 
     port: 80, 
     method: "POST", 
     path: fullPath, //gamma.firebase.com/... 
     agent: false, 
     headers: { 
      'content-type': 'application/json', 
      'Content-Length': body.length, 
     } 
    }; 
    var req = http.request(options, function(response) { 
     var result = ""; 
     console.dir(response.headers); 
     response.on('data', function(chunk) {    
      result+=chunk; 
     }); 
     response.on('end', function() { 
      console.error("POST response result: "+result); 
      try { 
       callback(JSON.parse(result)); 
      } catch(e) { 
       callback({ error: e }); 
      } 
     }); 
     response.on('error', function(e) { 
      console.error("POST response error: "+error); 
      callback({error: e}); 
     }); 
    }); 
    req.on('error', function(error) { 
     console.error("POST request error: "+error); 
    }); 
    req.write(body); 
    req.end(); 
} 

「對象」的內容可以是簡單的:

{"text": "test\u00a0text"} 

結果我得到的回覆是狀態200,和看起來看起來很合理的孩子名字,它實際上並沒有被創建。

我試着將content-type設置爲一堆不同的東西(例如,添加; charset =「UTF-8」),它似乎並沒有影響結果。

回答

2

我們處理某些類型的輸入時會產生錯誤的200狀態,這是錯誤的。我們將盡快推出一個修補程序。要同時解決此問題,您可以省略發送Content-Length標題。這將允許您發佈ASCII和非ASCII數據。