简体   繁体   中英

Node.js - Can't post nested/escaped JSON to body using Fermata REST client

The problem may be with the actual client, but he's not responding on github, so I'll give this a shot!

I'm trying to post, in the body, nested JSON:

{
   "rowkeys":[
      {
         "rowkey":"rk",
         "columns":[
            {
               "columnname":"cn",
               "columnvalue":"{\"date\":\"2011-06-21T00:53:10.309Z\",\"disk0\":{\"kbt\":31.55,\"tps\":6,\"mbs\":0.17},\"cpu\":{\"us\":5,\"sy\":4,\"id\":90},\"load_average\":{\"m1\":0.85,\"m5\":0.86,\"m15\":0.78}}",
               "ttl":10000
            },
            {
               "columnname":"cn",
               "columnvalue":"cv",
               "ttl":10000
            }
         ]
      },
      {
         "rowkey":"rk",
         "columns":[
            {
               "columnname":"cn",
               "columnvalue":"fd"
            },
            {
               "columnname":"cn",
               "columnvalue":"cv"
            }
         ]
      }
   ]
}

When I remove the columnvalue's json string, the POST works. Maybe there's something I'm missing regarding escaping? I've tried a few built in escape utilities to no avail.

var jsonString='the json string above here';

var sys = require('sys'),
      rest = require('fermata'), // https://github.com/andyet/fermata
         stack = require('long-stack-traces');

        var token = ''; // Username
        var accountId = ''; // Password

        var api = rest.api({
             url : 'http://url/v0.1/',
             user : token,
             password : accountId
        });

        var postParams = {
             body: jsonString
        };

        (api(postParams)).post(function (error, result) {
               if (error)
                    sys.puts(error);    

           sys.puts(result);
        });

The API I'm posting to can't deserialize this.

{
   "rowkeys":[
      {
         "rowkey":"rk",
         "columns":[
            {
               "columnname":"cn",
               "columnvalue":{
                  "date":"2011-06-21T00:53:10.309Z",
                  "disk0":{
                     "kbt":31.55,
                     "tps":6,
                     "mbs":0.17
                  },
                  "cpu":{
                     "us":5,
                     "sy":4,
                     "id":90
                  },
                  "load_average":{
                     "m1":0.85,
                     "m5":0.86,
                     "m15":0.78
                  }
               },
               "ttl":10000
            },
            {
               "columnname":"cn",
               "columnvalue":"cv",
               "ttl":10000
            }
         ]
      },
      {
         "rowkey":"rk",
         "columns":[
            {
               "columnname":"cn",
               "columnvalue":"fd"
            },
            {
               "columnname":"cn",
               "columnvalue":"cv"
            }
         ]
      }
   ]
}

Dual problems occuring at the same occurred led me to find an issue with the fermata library handling large JSON posts. The JSON above is just fine!

I think the real problem here is that you are trying to post data via a URL parameter instead of via the request body.

You are using Fermata like this:

path = fermata.api({url:"http://example.com/path");
data = {key1:"value1", key2:"value2"};
path(data).post(callback);

What path(data) represents is still a URL, with data showing up in the query part. So your code is posting to "http://example.com/path/endpoint?key1=value1&key2=value2" with an empty body.

Since your data is large, I'm not surprised if your web server would look at such a long URL and send back a 400 instead. Assuming your API can also handle JSON data in the POST body, a better way to send a large amount of data would be to use Fermata like this instead:

path = fermata.api({url:"http://example.com/path");
data = {key1:"value1", key2:"value2"};
path.post(data, callback);

This will post your data as a JSON string to "http://example.com/path" and you would be a lot less likely to run into data size problems.

Hope this helps! The "magic" of Fermata is that unless you pass a callback function, you are getting local URL representations, instead of calling HTTP functions on them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM