Reputation: 6422
Having trouble getting a WCF service to work with a HTML form post. I'm creating an SVGToPng service. The service accepts a String (SVG data) and converts it into an image to download (with save file dialogue). Right now all of the existing services are configured to use JSON as the message type. This particular method will be unique whereas I need to perform a good-old-fashioned form POST.
Here's the interface for the service.
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = UriTemplate.SvgExportPng, BodyStyle = WebMessageBodyStyle.Bare)]
Stream ExportSvgToPng(String svgData);
For the sake of testing I'm having the service just read an existing image file and return it (just to test the service). Here's the code.
WebOperationContext.Current.OutgoingResponse.ContentType = "image/png";
WebOperationContext.Current.OutgoingResponse.Headers.Clear();
WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-disposition", "attachment; filename=export-" + DateTime.Now.ToString("MM-dd-yyyy") + ".png");
return File.OpenRead(@"C:\tmp.png");
In my javascript I dynamically create the form, add the values I need, POST the form, then remove it from the document. Here's the javascript.
form.setAttribute("method", "POST");
form.setAttribute("action", Daedalus.Current.WcfUrl + '/svg/png');
hiddenField.setAttribute("name", "svgData");
hiddenField.setAttribute("value", view.trend.getSVG());
form.appendChild(hiddenField);
document.body.appendChild(form);
form.submit();
document.body.removeChild(form);
Finally here is the error message I'm receiving in my WCF log files.
The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.
Any help in much appreciated, thanks in advance.
Upvotes: 0
Views: 3191
Reputation: 87293
Your operation expects a string - and in a certain format, which is either a JSON string (with content type applicaiton/json) or as XML wrapped in a <string>
element (with the serialization namespace) and content type text/xml (or application/xml). The problem is that the form POST is sending forms/url-encoded data (content type application/x-www-form-urlencoded).
WCF doesn't support forms-urlencoded out-of-the-box, but you can either get the "jQuery support" from http://wcf.codeplex.com which has some classes to support it, or take the input as a Stream (like you do with the output) and parse the forms/urlencoded data yourself.
Upvotes: 1