wso2/jaggery

Bad content parsing when using nested JSON objects in post()

jparreira opened this issue · 3 comments

If one runs:

<%
var url = 'http://jaggeryjs.org/apidocs/resources/data.jag';
var result = post(url,
{"key": {
"primary":"foo",
"secondary":"bar"}
},
{
"User-Agent" : "Jaggery-XHR",
"Country" : "LK"
},
'json');
print(result.data);
%>

at http://jaggeryjs.org/apidocs/post.jag "try it" feature, gets as result:

{"method" : "POST", "url" : "http://jaggeryjs.org/apidocs/resources/data.jag", "content" : "key=%5Bobject%20Object%5D", "headers" : {"host" : "jaggeryjs.org", "user-agent" : "Jaggery-XHR", "country" : "LK", "x-forwarded-for" : "204.13.82.231", "x-forwarded-host" : "jaggeryjs.org", "x-forwarded-server" : "jaggeryjs.org", "connection" : "Keep-Alive", "content-length" : "25"}}

The content posted is key=%5Bobject%20Object%5D instead of
{"key": { "primary":"foo", "secondary":"bar"} }

I'll am investigating this issue.
but anyway, your requirement doesn't seems to be addressed with this code snippet regardless of the issue.
to send a json string as the POST's content, set the data as a string.

<%
var url = 'http://jaggeryjs.org/apidocs/resources/data.jag';
var data = stringify({"key": {
    "primary": "foo",
    "secondary": "bar"}
});
var result = post(url, data,
        {
            "User-Agent": "Jaggery-XHR",
            "Country": "LK"
        },
        'json');
print(result.data);
%>

If the REST service does a JSON parse it'll work. But since the post() data parameter can be a string or an object I guess Jaggery should handle the Object option better with support for nested Objects.

Yes, ideally following Jaggery code

<%
var url = 'http://jaggeryjs.org/apidocs/resources/data.jag';
var data = {"key": {
    "primary": "foo",
    "secondary": "bar"}
};
var result = post(url, data,
        {
            "User-Agent": "Jaggery-XHR",
            "Country": "LK"
        },
        'json');
print(result.data);
%>

should send a POST with following content

key%5Bprimary%5D=foo&key%5Bsecondary%5D=bar

I will provide a fix for this. FIY, post() method in Jaggery is modeled after jQuery.post