tags:

views:

330

answers:

2

I am having trouble working out what my XML should look like when performing a post request through WCF REST services. When using a datacontract i have no problem at all but when i just want to send across one parameter for example an int, i get the following error - "The remote server returned an error: (405) Method Not Allowed. "

[OperationContract]  
[WebInvoke(UriTemplate = "/DeleteUser", Method= "Post")]  
bool DeleteUser(int userId);

What should my XML look like?

Thanks in advance

A: 

like this

[OperationContract]
[WebInvoke(UriTemplate = "/DeleteUser/{userId}", Method= "Post")]
bool DeleteUser(string userId)
{
   int actualUserId = Int32.Parse(userId);
   ...
}

ps: Why are you using POST with a single parameter?


I can see not using GET if you want to Delete, but then why not use HTTP DELETE ? In this case the URI Template would be /user/{userId} and the Method = "Delete".

There's no payload, no XML to pass.

Rob Bagby explains

the code would look like

[OperationContract]
[WebInvoke(UriTemplate = "/User/{userId}", Method= "Delete")]
bool DeleteUser(string userId)
{
   int actualUserId = Int32.Parse(userId);
   ...
}
Cheeso
Thanks Cheeso ill use the DELETE for this. But for curiosity sake, if i was to create a user or something similar with just the 1 parameter, what would the xml look like?
DJ
To create a user, you'd have to define the schema for user (what are the datafields? firstname, lastname, etc). Then the XML would likely be <user><firstname>DJ</firstname><lastname>Dozier</lastname></user> . And you'd use POST, with a RequestFormat=WebMessageFormat.Xml .
Cheeso
I understand that if i use a datacontract thats what the xml would look like. But is it possible to post data without using a data contract.For example if this was the operationcontract - bool Addnumber(int number)what would the xml look like?
DJ
I think you need to ask a new question
Cheeso
that is what my original question was.
DJ
IF there's just a single param, it can be in the URL path, there's no XML. just like the first answer I have, above.
Cheeso
A: 

Im using a post as its a delete function so i dont want to use a parameter in the uri. Get calls should be for getting data.

Thats not the problem. The userId is an int and is expecting and int. My question is if doing a post using one parameter what should the xml look like?

DJ