Support for Form-UrlEncoded data in WCF Rest services

Some people have been asking around in the MSDN forums or stackoverflow about a possible way to pass form-urlencoded data to an existing WCF REST service. Although this is not a core feature in WCF from my point view, there is still some support for addressing this scenario in the REST Starter kit.

This feature takes the form of a new message formatter, FormsPostDispatchMessageFormatter, which is automatically injected to any existing service hosted within the new WebServiceHost2 host (shipped as part of the kit)

This formatter basically intercepts all messages, and handle those that contains “application/x-www-form-urlencoded” as content type in the request header. The rest of the messages are handled by the existing formatters as usual.

The service operation must expose a NameValueCollection argument in the method signature in order to receive the data. This collection will contain the key/value pairs that were parsed from request body.

For example,

[WebInvoke(UriTemplate = "SetData")]

[OperationContract]

void SetData(NameValueCollection formsData)

{

    this.value = Int32.Parse(formsData["counter"]);

}

“Counter”  in this case represents a field in the form that was posted to the service.

<

FORM action="service.svc/SetData" method="post">

Input Counter value and hit enter:

<INPUT type="text" name="Counter">

</FORM>

1 Comment

  • The WCF REST Contrib library enables this functionality:

    http://wcfrestcontrib.codeplex.com/

    It includes a POX formatter and form url encoded formatter and allows you to easily create your own. Formatters are mapped to mime types and automatically selected to serialize/deserialize the entity body based on the content type and accept headers.

Comments have been disabled for this content.