This is a migrated thread and some comments may be shown as answers.

DataSource pb with POST raw data

0 Answers 185 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Nicolas
Top achievements
Rank 1
Nicolas asked on 31 Jul 2012, 10:51 AM
Hi,

I want to use DataSource to connect to a REST API. But when I use DataSource to post raw data the body is changed just before the HTTP request, which fails due to bad request format. When I do the same with JQuery there is no problem. Here is a code snippet that illustrate that:

var postUrl = 'http://localhost:8080/catalog/devices';
var postBody = '{"model":"765-VVH-78"}';
 
var dataSource = new kendo.data.DataSource({
    transport: {
        create: {
            url: postUrl,
            type: 'POST',
            contentType: "application/json",
            data: postBody
        }
    }
});
 
var kendoPost = function() {
    dataSource.transport.create();
}
 
var jqueryPost = function() {
    $.ajax({
        url: postUrl,
        type: 'POST',
        contentType: "application/json",
        data: postBody
    });
}

I attach a screenshot of Firebug showing the results. "jqueryPost" post my raw data unchanged but "kendoPost" convert it before sending the HTTP request.

So I dig the kendo source code with firebug and I find where this happens. It's in "setup" method of the RemoteTransport class, at the following line:
parameters = extend(true, {}, data, options.data);
At this point "data" contains the raw data which is a string and not an object. And when a string is given to "extend" it is seen as an object, i.e. an array of characters, which is then converted to an object. Example: the call "$.extend({}, 'str')" returns {0: 's', 1: 't', 2: 'r'}.

So to ensure my raw data is not convert before the POST, I have modified the "setup" method for RemoteTransport class in kendo.web.js:
if (typeof data == "string") {
    parameters = data;
} else {
    parameters = extend(true, {}, data, options.data);
}

This way DataSource POST works as I expect.

But am I missing something? Is there another way to do what I want? Or is this a missing feature?

Thanks!

No answers yet. Maybe you can help?

Tags
General Discussions
Asked by
Nicolas
Top achievements
Rank 1
Share this question
or