tags:

views:

243

answers:

2
+1  Q: 

WCF REST POST XML

Here is a code snippet. Please tell me whats the difference between these two codes and also which content suitable for these code snippets. "application/xml" or "plain/text"

[OperationContract]
[WebInvoke(Method="POST", UriTemplate="DoSomething")]
public XElement DoSomething(XElement body) {
    ...
    return new XElement("Result");
}

[OperationContract]
[WebInvoke(Method="POST", UriTemplate="DoSomething")]
public string DoSomething(string body) {
    ...
    return "thanks";
}
A: 

Both methods respond to a POST request on a URI of the format '{BASE_URI}/DoSomething' (just a guess)

  • The first one expects some XML while the second one expects a string (in the body of the HTTP POST request).
  • The first one sends back some XML data () while the second one sends back a string ('thanks')

Regarding the 'content-type' setting: application/xml for the first one and plain/text for the second one.

Kenji Baheux
+1  A: 

WCF thinks everything by default is XML so both endpoints will return XML. The second one will return

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"&gt;thanks&lt;/string&gt;

With the content type application/xml. And if you want POST a string to it, you will have to send it a XML serialized string. Goofy isn't it.

If you really want to return just a string, then use Stream as your return type. Or take a look at WCF in .Net 4. It looks like they made it a whole lot easier to return other types.

Darrel Miller