asp.net web api - Error on data posting in xamarin.forms -
getting error while posting data sql using .net web api in xamarin.forms
statuscode: 204, reasonphrase: 'no content', version: 1.1, content: system.net.http.streamcontent, headers:{cache-control: no-cache pragma: no-cache server: microsoft-iis/8.5 x-aspnet-version: 4.0.30319 x-powered-by: asp.net access-control-allow-headers: content-type access-control-allow-methods: get, post, put, delete, options date: thu, 17 mar 2016 08:32:28 gmt expires: -1 }}
this code post data
t returnresult = default(t); httpclient client = null; try { client = new httpclient(); client.baseaddress = new uri(hostname); client.defaultrequestheaders.add("accept", "application/json"); client.timeout = new timespan(0, 0, 15); httpresponsemessage result = null; stringcontent data = null; if (content != null) // data = new stringcontent(jsonconvert.serializeobject(content), utf8encoding.utf8, "application/json"); data = new stringcontent(jsonconvert.serializeobject(content), encoding.utf8, "application/json"); if (method == httpmethod.get) result = await client.getasync(endpoint); if (method == httpmethod.put) result = await client.putasync(endpoint, data); if (method == httpmethod.delete) result = await client.deleteasync(endpoint); if (method == httpmethod.post) result = await client.postasync(endpoint, data); if (result != null) { if (result.issuccessstatuscode && result.statuscode == system.net.httpstatuscode.ok) { var json = result.content.readasstringasync().result; returnresult = jsonconvert.deserializeobject<t>(json); } }
where should problem ?. api working fine , enabled cors
here example of how using postasync in code, think error 'no content' referring on not sending server maybe? hope example helps:
public async task<bool> postappointmet(appointmententity anappointment) { try{ httpclient client = new httpclient(); client.defaultrequestheaders.add("authorization", "bearer " + app.apitoken); const string resourceuri = apibaseaddress + "/citas"; string postbody = jsonconvert.serializeobject(anappointment); client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); httpresponsemessage response = await client.postasync (resourceuri, new stringcontent (postbody, encoding.utf8, "application/json")); if (response.issuccessstatuscode) { return true; } return false; } catch{ return false; } }
where appointmententity model:
public class appointmententity { public int doctor { get; set; } public patiententity paciente { get; set; } public dateentity cita { get; set; }... }
Comments
Post a Comment