Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
C#
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare,     ResponseFormat = WebMessageFormat.Json, UriTemplate = "/insertcomplaint")]
Stream InsertComplaint(ComplaintData data);

Class (Complaints.cs)
C#
public Stream InsertComplaint(ComplaintData data)
{
    //the code
}

DataContract class (ComplaintData)
C#
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)
C#
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.

What I have tried:

With no success I tried changing the BodyStyle
C#
WebMessageBodyStyle.WrappedRequest

I also tried sending the json request as a string but that resulted in input paramater as null in the post method.
Posted
Updated 12-Nov-16 23:04pm
v3

1 solution

I solved this myself
In my client i changed from
C#
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;
            }
    }


to
C#
void PostComplaint()
    {
    string url = "http://localhost:28522/Complaints.svc/insertcomplaint";
               Uri uri = new Uri(url);

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

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

//Added
               var iData = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(new { ComplaintID = txtComplaintID.Text, EntryBy = txtEntryBy.Text });
//
               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;
               }
}
 
Share this answer
 
Comments
Member 11181064 29-Nov-18 3:08am    
Thanks for your post. This save my day.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900