DataSource pb with POST raw data

1 posts, 0 answers
  1. Nicolas
    Nicolas avatar
    1 posts
    Member since:
    Jul 2012

    Posted 31 Jul 2012 Link to this post

    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!
Back to Top