How to send XML payload in a web service call

1,410 views
Skip to first unread message

fledgling012

unread,
Nov 21, 2011, 4:10:13 PM11/21/11
to ServiceStack .NET Open Source REST Web Services Framework
Hi,

I am new to the domain of web services and have started using
ServiceStack with MONO. I was able to setup a self hosted web services
and implemented couple of GET calls using that. Now I am trying to
implement POST and PUT calls and want to send XML payload in that. I
would really appreciate if anyone could give pointers on how to go
about implementing that. The given ServiceStack samples are more
ASP.NET centric. I am using C# for all the implementation and using
the programmatic ways of ServiceStack. Right now I am trying to
implement a POST call like this
POST <host-uri>/client, and the following as XML.
<ClientName>abc</ClientName>
<ClientDetails>details</ClientDetails>

Thank you for the help.

Regards.

Demis Bellot

unread,
Nov 21, 2011, 4:39:21 PM11/21/11
to servic...@googlegroups.com
So the normal way to call ServiceStack's XML web service is to still use the strong typed generic Xml client, i.e.

var serviceClient = new XmlServiceClient("http://host/baseurl");
var response = serviceClient.Post<ClientResponse>(
new Client {ClientName = "Name", ClientDetails = "..."});

Doing this will send a Serialized XML Client DTO to your ServiceStack Client service.
I'd recommend using something like fiddler (or wireshark) so you can monitor the traffic and see the exact xml that gets sent.

Instead of using a typed client you can also send plain xml, see this StackOverflow answer for an example of sending raw Xml with a WebClient:

Cheers,

fledgling012

unread,
Nov 22, 2011, 8:03:04 AM11/22/11
to ServiceStack .NET Open Source REST Web Services Framework
Thank you Demis for your prompt reply. I really appreciate it. I
apologize if I am asking too many questions. I have another
application the functionalities of which I would like to expose via
ServiveStack so that users can query for information. The users are
going to choose some REST client like Poster by FireFox to trigger
calls. I looked at the Movies related example and in that if I want to
do a POST to add a new movie the payload needs to be like this

<Movies xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.servicestack.net/types">
<Id>String</Id>
<Movie>
<Director>String</Director>
<Genres xmlns:d3p1="http://schemas.microsoft.com/2003/10/
Serialization/Arrays">
<d3p1:string>String</d3p1:string>
</Genres>
<Id>String</Id>
<Rating>0</Rating>
<ReleaseDate>0001-01-01T00:00:00</ReleaseDate>
<TagLine>String</TagLine>
<Title>String</Title>
</Movie>
</Movies>

Is there a way the same can be done be removing things like xmlns
attribute or like this

<Movies>
<Id>String</Id>
<Movie>
<Director>String</Director>
<Genres>String</Genres>
<Id>String</Id>
<Rating>0</Rating>
<ReleaseDate>0001-01-01T00:00:00</ReleaseDate>
<TagLine>String</TagLine>
<Title>String</Title>
</Movie>
</Movies>

Such type of payload will be easier for users to provide. Again, thank
you for your time to answer my previous query.

Regards.


On Nov 21, 4:39 pm, Demis Bellot <demis.bel...@gmail.com> wrote:
> So the normal way to call ServiceStack's XML web service is to still use
> the strong typed generic Xml client, i.e.
>
> var serviceClient = new XmlServiceClient("http://host/baseurl");
>
> > var response = serviceClient.Post<ClientResponse>(
> > new Client {ClientName = "Name", ClientDetails = "..."});
>

> Doing this will send a Serialized XML Client DTO to your ServiceStack *
> Client* service.


> I'd recommend using something like fiddler (or wireshark) so you can
> monitor the traffic and see the exact xml that gets sent.
>
> Instead of using a typed client you can also send plain xml, see this

> StackOverflow answer for an example of sending raw Xml with a WebClient:http://stackoverflow.com/questions/8046538/json-format-data-from-cons...<http://stackoverflow.com/questions/8046538/json-format-data-from-cons...>
>
> Cheers,

Demis Bellot

unread,
Nov 22, 2011, 12:56:26 PM11/22/11
to servic...@googlegroups.com
Hi,

Unfortunately the "cleanliness" and verbosity of the XML namespaces generated/required is at the mercy of .NET's Xml DataContractSerializer that is used.

The best way to reduce the namespace cruft is to set a global namespace for all your DTO's.

You can do this globally without adding the [DataContract(Namespace=)] for all your DTOs by adding this in your AssemblyInfo.cs (you need to do this once for each namespace your DTOs are in) e.g:
(An example of this is in the Examples project at the bottom of AssemblyInfo.cs).

[assembly: ContractNamespace("http://schemas.servicestack.net/types", ClrNamespace = "My.Namespace.Where.My.DTOs.are")]

By default "http://schemas.servicestack.net/types" is used, and unless you care enough to change it I would leave it at that, and just use the above method to set the namespace for all your DTOs otherwise it will use a temporary generated namespace based on your C# namespace.
If you chose a namespace other than the default (i.e. http://schemas.servicestack.net/types) you will also need to set it your AppHost config like:

SetConfig(new EndpointHostConfig {
WsdlServiceNamespace = "http://my.custom.ns/types"
});

Now to get rid of the ugliness of the XML DataContract you need to decorate the collection classes used in your DTOs - in this case you would need a ArrayOfGenres, for an example of this see:

If you use the same namespace, it will remove the need for the cruft from the message.

BTW the xmlns:i="http://www.w3.org/2001/XMLSchema-instance" shouldn't be required when sending the DTO if it's not used.

Hope this helps.

Cheers
Reply all
Reply to author
Forward
0 new messages