none
ChangeInterceptor access incoming Data

    Question

  • I have a changeInterceptor and I would like to access and modify the object submitted via the PUT verb. The interceptor only exposes the object that will be updated but not the incoming object.

    For instance if I wanted to validate and scrub all incoming values before I updated my datasource.

    Is there a common property I can use for this? 

    Friday, June 18, 2010 5:13 PM

Answers

  • This was actually a misuse of the OData protocol. I wanted to only pass the values back that I wanted to update. To do this you need to use the MERGE HTTP action (and I think PATCH is coming soon). But, since some browsers don't like verbs like MERGE you can tunnel them through a POST. You just set the X-HTTP-Method header to the desired action.

    Here is an example using jQuery to update a WCF Data Service.

    updateRequirement: function (id, callback) {
          var url = "Services/MyService.svc/Requirements(" + id + ")";
          var r = new Object();
          r.Title = $("#txtTitle").val();
          r.Type = $("#reqType").val();
          r.AdmissionMethod = $("#ddlAdmissionMethods").val();
          r.Description = $("#editorPrereq").html();
    jr = JSON.stringify(r);

    UI.ShowLoading("Updating Requirement"); $.ajax({ beforeSend: function (xhrObj) { xhrObj.setRequestHeader("X-HTTP-Method", "MERGE"); } }, type: "POST", url: url,
    data:jr;
    dataType:json, contentType: "application/json; charset=utf-8", success: function (result) { callback(result); UI.HideLoading("Deleted!", 2500); }, error: function (msg) { UI.HideLoading("ERROR!", 4000); } }); },

    More information about this:
    Support for PATCH: http://blogs.msdn.com/b/pablo/archive/2010/06/14/http-patch-support-for-odata-in-wcf-data-services.aspx
    Tunneling through POST: http://www.odata.org/developers/protocols/operations#MethodTunnelingthroughPOST

    Hope this helps someone with the same problem. :)

    Luke

    • Marked as answer by Luke_Adams Wednesday, June 23, 2010 10:20 PM
    Wednesday, June 23, 2010 10:20 PM

All replies

  • Hi,

    Could you please specify the incoming values?  In the ChangeInterceptor, we can access the updated entities. 

    Thanks
    Pam

    Tuesday, June 22, 2010 9:39 AM
  • This was actually a misuse of the OData protocol. I wanted to only pass the values back that I wanted to update. To do this you need to use the MERGE HTTP action (and I think PATCH is coming soon). But, since some browsers don't like verbs like MERGE you can tunnel them through a POST. You just set the X-HTTP-Method header to the desired action.

    Here is an example using jQuery to update a WCF Data Service.

    updateRequirement: function (id, callback) {
          var url = "Services/MyService.svc/Requirements(" + id + ")";
          var r = new Object();
          r.Title = $("#txtTitle").val();
          r.Type = $("#reqType").val();
          r.AdmissionMethod = $("#ddlAdmissionMethods").val();
          r.Description = $("#editorPrereq").html();
    jr = JSON.stringify(r);

    UI.ShowLoading("Updating Requirement"); $.ajax({ beforeSend: function (xhrObj) { xhrObj.setRequestHeader("X-HTTP-Method", "MERGE"); } }, type: "POST", url: url,
    data:jr;
    dataType:json, contentType: "application/json; charset=utf-8", success: function (result) { callback(result); UI.HideLoading("Deleted!", 2500); }, error: function (msg) { UI.HideLoading("ERROR!", 4000); } }); },

    More information about this:
    Support for PATCH: http://blogs.msdn.com/b/pablo/archive/2010/06/14/http-patch-support-for-odata-in-wcf-data-services.aspx
    Tunneling through POST: http://www.odata.org/developers/protocols/operations#MethodTunnelingthroughPOST

    Hope this helps someone with the same problem. :)

    Luke

    • Marked as answer by Luke_Adams Wednesday, June 23, 2010 10:20 PM
    Wednesday, June 23, 2010 10:20 PM