[英]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! 问题可能出在实际的客户端上,但是他没有在github上响应,所以我给它一个机会!
I'm trying to post, in the body, nested JSON: 我试图在体内发布嵌套的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. 当我删除columnvalue的json字符串时,POST即可工作。 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. 我发布的API无法对此进行反序列化。
{
"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. 同时发生的双重问题导致我在处理大型JSON帖子的fermata库中发现了一个问题。 The JSON above is just fine! 上面的JSON很好!
I think the real problem here is that you are trying to post data via a URL parameter instead of via the request body. 我认为真正的问题是您试图通过URL参数而不是通过请求正文发布数据。
You are using Fermata like this: 您正在像这样使用Fermata:
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. path(data)
表示的仍然是URL, data
显示在查询部分中。 So your code is posting to "http://example.com/path/endpoint?key1=value1&key2=value2" with an empty body. 因此,您的代码将以空的正文发布到“ http://example.com/path/endpoint?key1=value1&key2=value2”。
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. 由于您的数据很大,因此如果您的Web服务器看到这么长的URL并发回400,我不会感到惊讶。 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: 假设您的API也可以处理POST正文中的JSON数据,则发送大量数据的更好方法是改用Fermata,如下所示:
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. 这会将您的数据作为JSON字符串发布到“ http://example.com/path”,并且您遇到数据大小问题的可能性将大大降低。
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. Fermata的“魔力”在于,除非传递回调函数,否则您将获得本地URL表示,而不是在它们上调用HTTP函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.