Search code examples
c#jsonwcfpostdata

WCF Post Method, returns 400 Bad Request


I see there are many answers to the same question but I am unable to resolve mine, can any one of you please go through my code and resolve my issue please. I have created a WCF Service as below The POST Method

Interface (iComplaints.cs)

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare,     ResponseFormat = WebMessageFormat.Json, UriTemplate = "/insertcomplaint")]
Stream InsertComplaint(ComplaintData data);

Class (Complaints.cs)

public Stream InsertComplaint(ComplaintData data)
{
    //the code
}

DataContract class (ComplaintData)

public class ComplaintData
{
   [DataMember]
    public string ComplaintID { get; set; }
    [DataMember]
    public string EntryBy { get; set; }
}

I have hosted the service locally and when i try to consume it using the below client method it's giving me 400 (Bad Request)

void PostComplaint()
    {
        HttpWebRequest req = null;
        HttpWebResponse res = null;
            string url = "http://localhost:28522/Complaints.svc/insertcomplaint";

            ComplaintData iData = new ComplaintData();
            iData.ComplaintID = txtComplaintID.Text;
            iData.EntryBy = txtEntryBy.Text;

            req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json"; 
            req.Headers.Add("SOAPAction", url);

            using (var streamWriter = new StreamWriter(req.GetRequestStream()))
            {
                streamWriter.Write(iData);
            }

            res = (HttpWebResponse)req.GetResponse();
            using (var streamReader = new StreamReader(res.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                TextBox1.Text = result;
            }
    }

Web Config of the WCF Service

<service behaviorConfiguration="ServiceBehavior" name="Complaints">
    <endpoint address="" behaviorConfiguration="webHttp" binding="webHttpBinding" contract="IComplaints" />
   <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
  </service>

Please anyone help.


Solution

  • i guess something is wrong with your serialization. please check this out:

            void PostComplaint()
        {
            HttpWebRequest req = null;
            HttpWebResponse res = null;
            string url = "http://localhost:28522/Complaints.svc/insertcomplaint";
    
            ComplaintData iData = new ComplaintData();
            iData.ComplaintID = txtComplaintID.Text;
            iData.EntryBy = txtEntryBy.Text;
    
            req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/json";
            req.Headers.Add("SOAPAction", url);
    
            using (var streamWriter = new StreamWriter(req.GetRequestStream()))
            {
                streamWriter.Write(Newtonsoft.Json.JsonConvert.SerializeObject(iData));
            }
    
            res = (HttpWebResponse)req.GetResponse();
            using (var streamReader = new StreamReader(res.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                TextBox1.Text = result;
            }
    
        }