c# - WebAPI POST, service always receiving null -
i've been searching hours problem can't seem find answer anywhere
i have built asp.net webapi accepts json get/post requests , works a1 when use fiddler or advanced rest client extension google chrome.
now have take existing android application , modify work webapi. i've cleared junk did not need make easier still can't post. works fine, receive response string , fine, post returns 400 bad request.
my webapi controller expects problemreports object. problemreports has been made me , goes :
public class problemreports { [key] public int problem_id { get; set; } [required] public nullable<int> request_type { get; set; } [required] public nullable<int> 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; } }
with subclasses
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; } }
and
public class contact_information { public string name { get; set; } public string phone { get; set; } }
here code android post in service handler class funny part of can put breakpoint before sending json string stringentity, copy raw json string, paste in body section advanced rest client, fill headers, hit post , boom : response 200 ok
if breakpoint webapi on controller, can see when sending request via advanced rest client, can access problemreports, if break on android app's request, problemreports null
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 web add problemreports: 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; }
here raw json string made app. actual string works fine on fiddler or advanced rest client
{ "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" }
}
i have no idea going on here, help/tip/advice do. i've set controller throw errors , thing 400 bad request
here post portion of controller
// post api/problemreports [responsetype(typeof(problemreports))] public ihttpactionresult postproblemreports([frombody]problemreports problemreports) { var allerrors = modelstate.values.selectmany(v => 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); }
found answer problems. took 4 hours find little error here goes :
i had line
httppost.setheader("content-type", "application/json; charset=utf-8")
and string entity
stringentity se = new stringentity(json);
it needed
httppost.setheader("content-type", "application/json")
and
stringentity se = new stringentity(json, "utf-8");
boom works fine, sorry bothering reading , have nice day
Comments
Post a Comment