Calling Rest Service (Post) from Kendo DataSource

1 Answer 749 Views
Data Source
Perry
Top achievements
Rank 1
Perry asked on 15 Feb 2019, 07:07 PM

I have a rest service that I can call in Postman

Type: Post

Header: Content-Type: application/json

Body:

{
"searchParam" :{

"from" : "2019-02-11T00:00:00Z",
"to" : "2019-02-16T00:00:00Z"
},
"filterParam":{
"deviceName":"1",
"devices":["111"],
"channels":[]
}
}

 

 

 

But is this the correct format for the kendo datasource for a post?  (with parameters).  I keep getting response errors when trying to connect.  

 

 

 

channelData: new kendo.data.DataSource({
    transport: {
      read: {
        url: "http://localhost:20212/api/",             
        type: "POST", 
        contentType: "application/json",         
        data: function() {
            return {
                searchParam: {
                    from: "2019-02-11T00:00:00Z",
                    to: "2019-02-15T00:00:00Z",
                },
                filterParam: {
                    deviceName: "1",
                           devices:"111"],
                    channels: [],
                }                  
            }             
         }        
    },        
 
    }
  }),

 

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Tsvetina
Telerik team
answered on 19 Feb 2019, 10:43 AM
Hello Perry,

If you need to post the request body in JSON format, you should use a parameterMap function to convert the parameters from query string format to JSON:
var channelData = new kendo.data.DataSource({
  transport: {
    read: {
      url: "https://localhost:20212/api/",            
      type: "POST",
      contentType: "application/json",        
      data: function() {
        return {
          searchParam: {
            from: "2019-02-11T00:00:00Z",
            to: "2019-02-15T00:00:00Z",
          },
          filterParam: {
            deviceName: "1",
            devices:["111"],
            channels: [],
          }                 
        }            
      }       
    },   
    parameterMap: function (options, type) {
      return kendo.stringify(options);
    }
  }
});

Apart from this, the DataSource configuration that you shared looks good to me, with the exception of a missing opening bracket but I assume this is a typo in the shared snippet:
filterParam: {
    deviceName: "1",
    devices:"111"],
    channels: [],
}


Regards,
Tsvetina
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Perry
Top achievements
Rank 1
commented on 19 Feb 2019, 08:40 PM

Thanks, ParameterMap was the missing piece.   Works now ;)
Tags
Data Source
Asked by
Perry
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Share this question
or