Click here to Skip to main content
15,892,809 members

Wcf post method, returns 400 bad request

NF Khan asked:

Open original thread
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.
Tags: C#, WCF, JSON

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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