2

I'm calling the Firebase REST API from a Node.js process. The problem I'm seeing is that POSTS fail when the post body contains non-ASCII characters. This is despite the request returning a "200" status, and the name of a node (which doesn't actually get created).

I'm currently trying something like this:

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();
}

The contents of "object" can be as simple as:

{"text": "test\u00a0text"}

The result I get back is status 200, and an reasonable-looking child name, which doesn't actually get created.

I've tried setting content-type to a bunch of different things (adding ; charset="UTF-8", for example), and it doesn't seem to affect the results at all.

Mark Bessey
  • 19,598
  • 4
  • 47
  • 69

1 Answers1

3

There is an error in the way we are handling certain types of input which is yielding the erroneous 200 status. We will roll out a fix shortly. To work around the problem in the meantime you can omit sending the Content-Length header. This will allow you to post ASCII and non-ASCII data.

  • Is a similar error still persisting in 2019? https://stackoverflow.com/questions/57178991/json-charset-problem-when-sending-to-firebase-from-js – Fanky Jul 24 '19 at 08:59