Let\'s say that I have specified the following WCF REST Service at the address \"http://localhost/MyRESTService/MyRESTService.svc\"
[ServiceContract]
public
So if you want to send raw data such as XML to your WCF REST Service (and also return), here is how to do it. But I have to say that before I found this solution I spend a lot of time googling frustrated as all examples were just talking about sending parameters in the URI (come on, the common scenario is to send XML and you can't do that properly in the URI). And when finally I found the right code examples it came up that it wasn't enough in WCF as I got the error "400 Bad Request". This error was caused by the fact that WCF can't use my raw XML if I don't force it by overriding it with some custom code (come on, what were you thinking Microsoft?, fix this in the next version of .NET Framework). So I'm not satisfied at all if doing such a basic thing can be so hard and time consuming).
** IMyRESTService.cs (server-side code) **
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare)]
Stream Receive(Stream text);
** client-side code **
XmlDocument MyXmlDocument = new XmlDocument();
MyXmlDocument.Load(FilePath);
byte[] RequestBytes = Encoding.GetEncoding("iso-8859-1").GetBytes(MyXmlDocument.OuterXml);
Uri uri = new Uri("http://localhost/MyRESTService/MyRESTService.svc/Receive");
Request.ContentLength = RequestBytes.Length;
Request.Method = "POST";
Request.ContentType = "text/xml";
Stream RequestStream = Request.GetRequestStream();
RequestStream.Write(RequestBytes, 0, RequestBytes.Length);
RequestStream.Close();
HttpWebResponse response = (HttpWebResponse)Request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string ResponseMessage = reader.ReadToEnd();
response.Close();
** XmlContentTypeMapper.cs (server-side custom code which forces WCF to accept raw XML) **
public class XmlContentTypeMapper : WebContentTypeMapper
{
public override WebContentFormat GetMessageFormatForContentType(string contentType)
{
return WebContentFormat.Raw;
}
}
** Web.config (server-side configuration settings for utilizing the custom code)
<endpoint binding="customBinding" bindingConfiguration="XmlMapper" contract="MyRESTService.IMyRESTService"
behaviorConfiguration="webHttp" />
<bindings>
<customBinding>
<binding name="XmlMapper">
<webMessageEncoding webContentTypeMapperType="MyRESTService.XmlContentTypeMapper, MyRESTService"/>
<httpTransport manualAddressing="true"/>
</binding>
</customBinding>
</bindings>
Invoke a WCF Web Service with an HTTP POST http://social.msdn.microsoft.com/forums/en-us/wcf/thread/4074F4C5-16CC-470C-9546-A6FB79C998FC
I had the same problem, and using Fiddler, I received a 403 (Bad Request) error every time I tried to post using the Body. The Fix, which took me a couple of hours to find is to change the Content Type to application/xml and send the body as an Xml.
This is the line I used in the Header in Fiddler:
Content-Type: application/xml;charset=UTF-8
Now I haven't used Microsoft.Http library, in my client I used WebRequest to do the post, and by setting the Content-Type to:
request.ContentType = "application/xml;charset=UTF-8";
Worked fine for me.
Now if you want to use the URI to send parameters I recommend using WebGet instead of WebInvoke with POST.