Reputation: 525
I have a web service with both SOAP and REST endpoints. I have to accept requests from a client that I do not have any control over on my REST endpoint. Currently the client gets a 400 response and the tracelog on my server shows this error:
The incoming message has an unexpected message format 'Raw'.
The expected message formats for the operation are 'Xml', 'Json'.
I've tried everything I can think of with the WebContentTypeMapper but seem to end up right where I started every time. The request from the client doesn't appear to be well formed XML or JSON so if I try to force XML or JSON types from the WebContentTypeMapper I end up with parser errors.
So I guess I need to find out if I can force this endpoint to accept the message anyway. That should be easy, right guys? ...guys? ...right?
Upvotes: 3
Views: 2672
Reputation: 6109
If you make the operation take a stream then you can analyse the incoming data yourself and work out what to do with it. The ContentType of the HTTP request should tell you what is in the stream
As an example, say you had a service which allowed you to upload images. You may want to perform different kinds of image processing depending on the image type. So we have a service contract as follows:
[ServiceContract]
interface IImageProcessing
{
[OperationContract]
[WebInvoke(Method="POST", UriTemplate = "images")]
void CreateImage(Stream stm);
}
The implementation checks the Content type of the request and performs processing dependent on it:
public void CreateImage(Stream stm)
{
switch(WebOperationContext.Current.IncomingRequest.ContentType)
{
case "image/jpeg":
// do jpeg processing on the stream
break;
case "image/gif":
// do GIF processing on the stream
break;
case "image/png":
// do PNG processing on the stream
break;
default:
throw new WebFaultException(HttpStatusCode.UnsupportedMediaType);
}
}
Upvotes: 9
Reputation: 44605
my understanding is that REST requires either JSON or XML, check here:
WCF (Windows Communication Foundation) is the new standard for building services with .NET and REST (REpresentational State Transfer) has been a very popular technique when building services and not just in the .NET space. With services, you transport serialized data across the pipe to be worked with and there are a few serialization methods:
Binary: Not for REST services, but provides the smallest packet size. Each end must explicity know how to handle the data.
SOAP: The long running standard for web services. Very descriptive, but a very large packet size due to the amount of meta data.
XML (POX): Plain Old XML provides the just the data with structure without the meta data leaving a smaller packet size.
JSON (JavaScript Object Notation): A new and up coming standard with a similar packet size to a plain XML feed, but can be used directly in JavaScript making it the best option when consuming a service from jQuery.
Upvotes: 0