1

I am writing a .NET app that will talk to JSON-based API to pull/push data. I saw similar question asked before: Consuming a RESTful JSON API using WCF

but I need little more information on the same subject. Here is JSON that I have to send in request:
{"login":{"password":"PASSWORD","username":"USERNAME"}}

and response JSON will be something like:
{"response":{"status":"OK","token":"o9b0jrng273hn0"}}

Here is what I came up with:

[ServiceContract]
public interface ITestApi
{
    [OperationContract]
    [WebInvoke( Method = "POST",
        BodyStyle = WebMessageBodyStyle.Wrapped,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "/login"
        )]
    LoginResponse Login( LoginRequest login );
}

where LoginRequest has username and password properties and LoginResponse has token property.

When I call the api, request is successful and I get the response back as expected ( I verified this with Fiddler). But WCF is not able to create LoginResponse object for me. it is always null. I believe I am doing somethign wrong, can someone please point me out what I have to do to get this right?

Is this the right way to create a JSON-based REST service client? I am using RESTful api first time, so I do not have more knowledge about it.

5
  • It all looks good. Your problem is in the implementation of the service. Post it here and we can help. Commented Nov 24, 2010 at 22:42
  • What does your LoginResponse class look like? Is it decorated with DataContract/DataMember attributes? Commented Nov 24, 2010 at 22:45
  • Here is LoginResponse class: public class LoginResponse { public string token { get; set; } } Commented Nov 24, 2010 at 22:51
  • Adding DataContract/DataMember did not help. Hre is my service implementation class: public class TestApiClient : ClientBase<ITestApi>, ITestApi { public LoginResponse Login( LoginRequest login) { return base.Channel.Login( login ); } } Commented Nov 24, 2010 at 23:03
  • did you get a solution for this.. I am facing same problem Commented Jan 5, 2011 at 21:30

2 Answers 2

0

Your LoginResponse class should look like something this:

[DataContract]
public class LoginResponse 
{ 
    [DataMember]
    public string token { get; set; } 
}

It needs to be decorated with the DataContract and DataMember attributes so the serializer (DataContractJsonSerializer in the case of JSON ) knows how to serialize it.

EDIT:

Also, your client should be configured to use webHttpBinding and the endpoint behavior should be configured to use webHttp, as in the following example.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok I read that article before and my binding and endpoint behavior are same as explained in that article. Now I added DataContract/DataMember attributes in LoginResponse, but still response object is null. Fiddler shows that my request was successful and I am getting token in http response, but WCF cannot able to parse the response back.
0

Go get the Microsoft.Http client library from the lib folder in this project or from thw WCF REST Starter Kit. Using this you can do:

var client = new HttpClient();
var content = HttpContent.Create("{'login':{'password':'PASSWORD','username':'USERNAME'}}", "application/json");
var response = client.Post("http://service.com/login",content);
var jsonString = response.Content.ReadAsString();

If you don't want to read the Json as a string and parse using something like Json.Net, and you prefer to use DataContracts, you can do:

var loginResponse = response.Content.ReadAsJsonDataContract<LoginResponse>();

Using WCF Channels on the client to deal with REST services is just going cause you far more pain than you really want. You are much better to just stick with plain HTTP.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.