Friday, October 28, 2016

Passing parameter containing special characters to web service methods

Web service does not process parameters with special characters as those can be candidates to dangerous inputs.

So as a workaround you can send the parameters as a query string. That said, the query string should not be too long since there are limitations

For illustration purposes, in the below example JavaScript call I send two variables id & date as method parameters. The address information which is included in variable address1 & address2 are send as  query string.

var restUrl = _spPageContextInfo.webAbsoluteUrl +
            "/_vti_bin/MyService.svc/MyMethod/" + id + "/" + date + "?address1=" + encodeURIComponent(address1) + "&address2=" + encodeURIComponent(address2) ;

Inside your web method access the query string as below:

string address = HttpContext.Current.Request.QueryString["address1"];

No comments:

Post a Comment