Calling a REST Based JSON Endpoint with HTTP POST and WCF

Posted by Wallym on ASP.net Weblogs See other posts from ASP.net Weblogs or by Wallym
Published on Sun, 16 Jan 2011 11:00:00 GMT Indexed on 2011/01/16 11:54 UTC
Read the original article Hit count: 561

Filed under:
|
|

Note: I always forget this stuff, so I'm putting it my blog to help me remember it.

Calling a JSON REST based service with some params isn't that hard.  I have an endpoint that has this interface:

        [WebInvoke(UriTemplate = "/Login",
            Method="POST",
            BodyStyle = WebMessageBodyStyle.Wrapped,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json )]
        [OperationContract]
        bool Login(LoginData ld);

The LoginData class is defined like this:

    [DataContract]
    public class LoginData
    {
        [DataMember]
        public string UserName { get; set; }
        [DataMember]
        public string PassWord { get; set; }
        [DataMember]
        public string AppKey { get; set; }
    }
 

Now that you see my method to call to login as well as the class that is passed for the login, the body of the login request looks like this:

{ "ld" : {  "UserName":"testuser", "PassWord":"ackkkk", "AppKey":"blah" } }

The header (in Fiddler), looks like this:

User-Agent: Fiddler
Host: hostname
Content-Length: 76
Content-Type: application/json

And finally, my url to POST against is:

http://www.something.com/...../someservice.svc/Login

And there you have it, calling a WCF JSON Endpoint thru REST (and HTTP POST)

© ASP.net Weblogs or respective owner

Related posts about wcf

Related posts about JSON