Click here to Skip to main content
13,255,238 members (53,826 online)
Rate this:
 
Please Sign up or sign in to vote.
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)
[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.

What I have tried:

With no success I tried changing the BodyStyle
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 12-Nov-16 5:55am
NF Khan412
Updated 13-Nov-16 0:04am
v3

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.171114.1 | Last Updated 13 Nov 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100