sunilr
sunilr

Reputation: 19

Bad request 400 on sending xml request in wcf rest

I am writing a sample application using wcf rest for authentication. Here is the snapshot of the code:

service Interface:

[ServiceContract]
public interface IAuthenticate
{
    [OperationContract]
    [WebInvoke(BodyStyle=WebMessageBodyStyle.Bare, 
Method = "POST", UriTemplate = "/VUser",RequestFormat= WebMessageFormat.Xml ), ]
    string CreateUser(VUser user);
}

Datacontract class:

[DataContract]
public class VUser
{
    public VUser()
    {
    }

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

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

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

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

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

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

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

    [DataMember]
    public int CountryCode { get; set; }
}

Service class:

public class Authenticate : IAuthenticate
{

    #region IAuthenticate members
    public string CreateUser(Vuser user)
    {
        //processing xml for response

    }
    #endregion IAuthenticate
}

client code:

       Uri baseAddress = new Uri("http://localhost:8000");

        using (WebServiceHost host = new WebServiceHost(typeof(Authenticate), baseAddress))
        {
            host.Open();
            Console.WriteLine("Press any key to terminate");
            Console.ReadLine();
            host.Close();

        }

Now I am using fiddler to send the request after host.open() and send the the request has shown:

post http://localhost:8000/Vuser/

User-Agent: Fiddler Host: localhost:8000 content-length: 233 content-type: text/xml

and in request body :

sandy r sunil sunil.r 919900101948 winter male 01

but it is returning me HTTP/1.1 400 Bad Request. My question is am I passing the vuser class correctly to the create user method or is there any other way to send the vuser.

Please help me.

Upvotes: 0

Views: 1271

Answers (3)

saravanan
saravanan

Reputation: 408

The Namespace in both client and server should match. Try to add namespace name as

[DataContract(Namespace = "http://sample.com")]
public class VUser

in the server contract. And then make sure the xml string has the xmlns value with the same namespace

"<VUser xmlns=\"http://sample.com" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">...</VUser>"

Upvotes: 0

user2064406
user2064406

Reputation: 1

Kindly specify The Datacontract Namespace in DataContract Class

[DataContract(Namespace = "http://xxx.xxx.xxx/Service.svc")]

and follow same in Xml file

Upvotes: 0

Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65481

It could be a problem with serialization.

Serialization uses the default consrtuctor, without parameters.

In C# the compiler will automatically create a default constructor, except if you create a constructor with a parameter.

The Authenticate class is missing a default constructor, you will therefore have probelms sending it over WCF.

Upvotes: 0

Related Questions