Calling a REST Based JSON Endpoint with HTTP POST and WCF
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)