Posting an HTML Form to a Simple Service => expected a value assignable to type 'String' for input parameter 'sku' but received a value of type 'HttpRequestMessage`1'.'.

Topics: Web Api
Jun 27, 2011 at 12:59 PM

I'm trying to do something I assumed would be extremely straightforward: Post a couple of HTML values from a form and receive those as simple type parameters in my resource. I must be missing something because I haven't been able to do it yet.

I'm getting hit with an exception:

Request Error

The server encountered an error processing the request. The exception message is 'The service operation 'AddToBasket' expected a value assignable to type 'String' for input parameter 'sku' but received a value of type 'HttpRequestMessage`1'.'. See server logs for more details. The exception stack trace is:

at Microsoft.ApplicationServer.Http.Dispatcher.OperationHandlerPipelineInfo.ServiceOperationHandler.ValidateAndConvertInput(Object[] values) at Microsoft.ApplicationServer.Http.Dispatcher.OperationHandlerPipelineInfo.GetInputValuesForHandler(Int32 handlerIndex, Object[] pipelineValues) at Microsoft.ApplicationServer.Http.Dispatcher.OperationHandlerPipeline.ExecuteRequestPipeline(HttpRequestMessage request, Object[] parameters) at Microsoft.ApplicationServer.Http.Dispatcher.OperationHandlerFormatter.OnDeserializeRequest(HttpRequestMessage request, Object[] parameters) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

Here's my resource:

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class BasketResource
    [WebInvoke(UriTemplate = "/AddToBasket", Method = "POST")]
    public HttpResponseMessage AddToBasket(string sku)
        string s = sku;

        return new HttpResponseMessage("done", HttpStatusCode.OK);

And here's my form

	<script type="text/javascript" src=""></script>
	<script type="text/javascript">
		$(function () {
			$("form").submit(function () {
					type: "POST",
					url: "services/basket/addtobasket",
					data: $(this).serialize(),
					success: function (data, textStatus, jqXHR) {
					error: function (xhr, status, error) {
					dataType: 'json'
				return false;
	<input type="input" name="sku" id="sku" value="123" />
	<input type="submit" value="Go" />