wso2 / jaggery

The delicious Javascript framework
Apache License 2.0
115 stars 175 forks source link

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

Open jparreira opened 10 years ago

jparreira commented 10 years ago

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"} }

manuranga commented 10 years ago

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);
%>
jparreira commented 10 years ago

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.

manuranga commented 10 years ago

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