Passing JSON Data to REST POST method
<p>I am trying to pass JSON data (present in file) to JSON POST method.</p>

<p>But getting HTTP 400 error (Bad Request) - "The remote server returned an error: (400) Bad Request."</p>

<p>Please help.</p>

<p><strong>Interface -:</strong></p>

<pre><code>[OperationContract]
[WebInvoke(UriTemplate = "/JSON", Method = "POST")]
string CreatePersonFromJSONString(Person createPerson);
</code></pre>

<p><strong>Implemented Function -:</strong></p>

<pre><code> public string CreatePersonFromJSONString(Person createPerson)
  {
     createPerson.ID = (++personCount).ToString();
     persons.Add(createPerson);
     return new JavaScriptSerializer().Serialize(createPerson);
  }
</code></pre>

<p><strong>Program -:</strong></p>

<pre><code> HttpWebRequest req = WebRequest.Create(uri) as HttpWebRequest;
 req.KeepAlive = false;
 req.Method = Method.ToUpper();

if (("POST,PUT").Split(',').Contains(Method.ToUpper()))
 {
   Console.WriteLine("Enter JSON FilePath:");
   string FilePath = Console.ReadLine();
   content = (File.OpenText(@FilePath)).ReadToEnd();

    req.ContentType = "application/json;";

    //initiate the request
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    var resToWrite = serializer.Deserialize&lt;Person&gt;(content);
    StreamWriter PostData = new StreamWriter(req.GetRequestStream());
    PostData.Write(resToWrite);
    PostData.Flush();
    PostData.Close();
}

  HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
</code></pre>

<p><strong>Input File Content -:</strong></p>

<pre><code>{  "Age":"25",
       "ID":"4",
       "Name":"Ashish" 
    }
</code></pre>

<p><strong>Person Class -:</strong></p>

<pre><code> [DataContract]
    public class Person
    {
        [DataMember]
        public string ID;
        [DataMember]
        public string Name;
        [DataMember]
        public string Age;
    }
</code></pre>

<blockquote>
  <p>Uri - <a href="http://localhost:5171/RestService/JSON" rel="nofollow">http://localhost:5171/RestService/JSON</a></p>
  
  <p>Method - POST</p>
</blockquote>