c# - Calling Web API using PostAsJsonAsync -
c# - Calling Web API using PostAsJsonAsync -
i newbie trying phone call web api method.
i have quadrilateral object:
public class quadrilateral { public double sidea { get; private set; } public double sideb { get; private set; } public double sidec { get; private set; } public double sided { get; private set; } public double angleab { get; private set; } public double anglebc { get; private set; } public double anglecd { get; private set; } public double angleda { get; private set; } public virtual string name { { homecoming "generic quadrilateral"; } } public virtual string description { { homecoming "a quadrilateral has no specific name shape"; } } public quadrilateral(double sidea, double sideb, double sidec, double sided, double angleab, double anglebc, double anglecd, double angleda) { sidea = sidea; sideb = sideb; sidec = sidec; sided = sided; angleab = angleab; anglebc = anglebc; anglecd = anglecd; angleda = angleda; } }
my web api post method simple. accepts quadrilateral object, , returns subtype of quadrilateral, rectangle:
public class rectangle : quadrilateral { public override string name { { homecoming "rectangle"; } } public override string description { { homecoming "a quadrilateral equal opposite sides , 4 right angles"; } } public rectangle(quadrilateral q) : base(q.sidea, q.sideb, q.sidec, q.sided, q.angleab, q.anglebc, q.anglecd, q.angleda) { } }
the web api post method looks this:
public quadrilateral post(quadrilateral q) { homecoming new rectangle(q); }
i create quadrilateral object, assigning members values. seek invoke web api using next code:
readonly string baseuri = "http://localhost:43798/"; readonly string controller = "api/quadrilateral"; using (httpclient client = new httpclient()) { client.baseaddress = new uri(baseuri); client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); httpresponsemessage response = await client.postasjsonasync(controller, quadrilateral); if (response.issuccessstatuscode) { homecoming await response.content.readasasync<quadrilateral>(); } else { homecoming null; } }
debugging shows web api post method hit, quadrilateral object contains 0.0 double members. why post method not getting object trying send it?
what json value posting? sure getting right json before posting?
try clearing take headers,
client.baseaddress = new uri(baseuri); client.defaultrequestheaders.accept.clear(); client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); httpresponsemessage response = await client.postasjsonasync(controller, quadrilateral);
c# asp.net asp.net-mvc post
Comments
Post a Comment