BentOnCoding
BentOnCoding

Reputation: 28148

How to setup WCF Rest GET?

I am using the WCF REST Template to test out REST Development. I am trying to test a POST Method defined like this:

    [WebInvoke(UriTemplate = "", Method = "POST")]
    public SampleItem Create(SampleItem instance)
    {
        // TODO: Add the new instance of SampleItem to the collection
        return new SampleItem() { Id = 1, StringValue = "Hello Post" };
    }

However I cant figure out how to get it to accept my SampleItem Parameter. I am using fiddler to submit a POST Request to http://ipv4.fiddler:54916/service1 with the following Header:

User-Agent: Fiddler
Host: ipv4.fiddler:54916

I have tried the following two solutions for the Request Body:

SampleItem.Id=1&SampleItem.StringValue=TestValue

AND

<SampleItem xmlns="http://schemas.datacontract.org/2004/07/PerTrac.Cloud.Service.Statistic">
  <Id>2147483647</Id>
  <StringValue>String content</StringValue>
</SampleItem>

However all I am able to get back is a 400 error.

SampleItem looks like this:

    // TODO: Edit the SampleItem class
    [DataContract]
    public class SampleItem
    {
        [DataMember]
        public int Id { get; set; }

        [DataMember]
        public string StringValue { get; set; }
    }

How can I pass SampleItem to the service without getting a 400 error ???

Upvotes: 0

Views: 393

Answers (1)

carlosfigueira
carlosfigueira

Reputation: 87218

The first example won't work (application/x-www-forms-urlencoded is not supported by WCF out-of-the-box). The second example should work if: 1) the namespace of the SampleItem class is PerTrac.Cloud.Service.Statistic; and 2) if you add a Content-Type header to the request (Content-Type: text/xml).

If it doesn't work, then try enabling tracing on the server side. The trace will have an information saying why the request is being rejected by WCF.

Upvotes: 3

Related Questions