title: WebAPI POST, service always receiving null
body: <p>I've been searching for hours about this problem but I can't seem to find any answer anywhere</p>
  
  <p>I have built an ASP.NET WebAPI that accepts JSON GET/POST requests and it works A1 when I use fiddler or advanced rest client extension for google chrome.</p>
  
  <p>Now I have to take an existing android application and modify it to work with my WebAPI. I've cleared all the junk I did not need to make it easier but I still can't POST.
  GET works fine, I receive my response string and everything is fine, but the POST returns a 400 Bad Request.</p>
  
  <p>My webApi controller expects a ProblemReports object. ProblemReports has been made by me and it goes like this :</p>
  
  <pre><code>    public class ProblemReports
  {
      [Key]
      public int problem_id { get; set; }
      [Required]
      public Nullable&lt;int&gt; request_type { get; set; }
      [Required]
      public Nullable&lt;int&gt; problem_type_id { get; set; }
      public String picture_url { get; set; }
      public contact_information contact_information { get; set; }
      public problem_location problem_location { get; set; }
      public bool dog_on_property { get; set; }
  
  }
  </code></pre>
  
  <p>with subclasses</p>
  
  <pre><code>    public class problem_location
  {
      public String street { get; set; }
      public String city { get; set; }
      public int relative_position_id { get; set; }
      public double longitude { get; set; }
      public double latitude { get; set; }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>    public class contact_information
  {
      public String name { get; set; }
      public String phone { get; set; }
  }
  </code></pre>
  
  <p>Here is the code for my android POST in my service handler class
  The funny part of this is that I can put a breakpoint before sending my json string to the StringEntity, copy the raw json string, paste it in the BODY section advanced rest client, fill the headers, hit post and boom : Response is 200 OK</p>
  
  <p>If I breakpoint my WebAPI on the controller, I can see that when sending the request via Advanced rest client, I can access problemreports, but if I break it on my android app's request, problemreports is null</p>
  
  <pre><code>public static String POST(String url, ProblemReports report) {
      InputStream inputStream = null;
      String result = "";
  
      if (report.picture_url == null)
      {
          report.picture_url = "no picture";
      }
      try {
  
          HttpClient httpclient = new DefaultHttpClient();
          HttpPost httpPost = new HttpPost(url);
          String json = "";
          JSONObject jsonObject = new JSONObject();
          jsonObject.accumulate("request_type", report.request_type);
          jsonObject.accumulate("problem_type_id", report.problem_type_id);
          jsonObject.accumulate("picture_url", report.picture_url);
  
          JSONObject contact_informationObject = new JSONObject();
          contact_informationObject.accumulate("name",
                  report.contact_information.name);
          contact_informationObject.accumulate("phone",
                  report.contact_information.phone);
          jsonObject.accumulate("contact_information",
                  contact_informationObject);
  
          JSONObject problem_locationObject = new JSONObject();
          problem_locationObject.accumulate("street",
                  report.problem_location.street);
          problem_locationObject.accumulate("city",
                  report.problem_location.city);
          problem_locationObject.accumulate("latitude",
                  report.problem_location.latitude);
          problem_locationObject.accumulate("longitude",
                  report.problem_location.longitude);
          problem_locationObject.accumulate("relative_position_id",
                  report.problem_location.relative_position_id);
          jsonObject.accumulate("problem_location", problem_locationObject);
  
          jsonObject.accumulate("dog_on_property", report.dog_on_property);
  
          json = jsonObject.toString();
          //String otherJson = "{ProblemReports: " + json + "}";
          //I saw on the web to add ProblemReports: but it doensn't work
  
          StringEntity se = new StringEntity(json);
          httpPost.setEntity(se);
          httpPost.setHeader("Accept", "application/json");
          httpPost.setHeader("Content-type", "application/json; charset=utf-8");
          httpPost.setHeader(
                  "Authorization",
                  "Bearer TokenRemovedBecauseUseless");
  
  
          HttpResponse httpResponse = httpclient.execute(httpPost);
          inputStream = httpResponse.getEntity().getContent();
  
          if (inputStream != null)
              result = convertInputStreamToString(inputStream);
          else
              result = "InputStream convert fail!!";
  
      } catch (Exception e) {
          return e.getCause().toString();
      }
  
      return result;
  }
  </code></pre>
  
  <p>Here is the raw JSON string made from my app. That actual string works fine on Fiddler or advanced rest client</p>
  
  <pre><code>{
  "contact_information":
  {
      "phone":"6666666666",
      "name":"fiber"
  },
  "dog_on_property":false,
  "problem_type_id":3,
  "request_type":1,
  "problem_location":
  {
      "longitude":1234,
      "latitude":1234,
      "relative_position_id":0,
      "city":"Montreal, QC A1A 1A1",
      "street":"0000 René-Lévesque Blvd"
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I have no idea about what is going on here, any help/tip/advice will do.
  I've set my controller to throw any errors and the only thing I get is 400 BAD REQUEST</p>
  
  <p>Here the post portion of my controller for </p>
  
  <pre><code> // POST api/ProblemReports
      [ResponseType(typeof(ProblemReports))]
      public IHttpActionResult PostProblemReports([FromBody]ProblemReports problemreports)
      {
          var allErrors = ModelState.Values.SelectMany(v =&gt; v.Errors);
          if (!ModelState.IsValid)
          {
              throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState));
            //  return BadRequest(ModelState);
          }
  
          try
          {
              db.ProblemReports.Add(problemreports);
              db.SaveChanges();
          }
          catch (Exception ex)
          {
              return Ok(ex);
          }
  
  
          //return CreatedAtRoute("DefaultApi", new { id = problemreports.problem_id }, problemreports);
          ReturnID ri = new ReturnID(problemreports.problem_id);
          return Ok(ri);
      }
  </code></pre>