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)
{
}
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.