2012-05-29 21 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数据。