DJ.
DJ.

Reputation: 21

What should an XML post request look like for WCF RESTful service with a single parameter?

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

Upvotes: 2

Views: 1662

Answers (4)

RakingTheLeaves
RakingTheLeaves

Reputation: 21

DJ -- The only way I've found to do what you were originally asking is to swipe the example from this link. It uses a Stream class to bring in the post parameters in the body of the HTTP request. Then you have to slog through it manually...

Hope this helps.

Upvotes: 1

Rajesh
Rajesh

Reputation: 7876

Make sure you are performing a POST operation to the resource. The URL might be as follows:

http://localhost/SampleApplication/DeleteUser

Below is the request format that you need to have

<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">55</int>  

The above xml needs to be part of the message body.

Upvotes: 0

Cheeso
Cheeso

Reputation: 192417

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);
   ...
}

Upvotes: 0

DJ.
DJ.

Reputation: 1

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?

Upvotes: 0

Related Questions