0

I am creating a wcf service that will be consumed by the android application.

I have declared an interface like:

[OperationContract]
    [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "RegsiterUser")]
    string RegsiterUser(RegsiterUser rUser);

In service code:

public string RegsiterUser(RegsiterUser rUser)
{
    return rUser.userName;
}

I have created a class:

[DataContract]
public class RegsiterUser
{

    #region Property

    [DataMember]
    public string userName { get; set; }
    [DataMember]
    public string Password { get; set; }
    #endregion
}

When this service is called, then it is giving bad request error: 400

I have also tested it using postmaster app of chrome and the same is coming, so if any body has a solution, please help me. Thanks.

Harshad Pansuriya
  • 17,218
  • 7
  • 58
  • 86
user2247651
  • 125
  • 1
  • 2
  • 11
  • Could you please post the JSON you are sending to the server? – jHilscher Nov 26 '13 at 18:33
  • You'll need to post the details of how you're calling the service, because we can't help you with this. Can you post your request code? – Ben Nov 26 '13 at 18:38

1 Answers1

1

Interface

Since you are using a GET-Method with an Parameter you need to declare that parameter in the UriTemplate: Get-Methods with a request-body can technically work, but are not recommended: see this post for more on this topic: HTTP GET with request body

So you could change the method to POST, and submit a JSON, or work with GET and parameters.

This would be an example for the parameter version:

    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "getuserdata/{userName}")]
     string RegsiterUser(string userName);

If you want POST, just change the Method to POST, the rest looks fine.

Model

I would recommend specifying the names of the properties, likes this: This way you get more control over your JSON and it's clear how the properties have to be named.

    [DataMember(Name = "userName")]
    public string UserName { get; set; }
Community
  • 1
  • 1
jHilscher
  • 1,690
  • 2
  • 23
  • 27