I've been working on both the backend and the frontend of a mobile application and am having trouble getting my post requests to work properly. I've been using Xamarin.Forms for the frontend and .Net for the backend.
The code on the client side:
var res = await App.Client.PostAsync("url", args);
var s =await res.Content.ReadAsStringAsync();
await DisplayAlert("Content", s, "OK");
I have checked and I am receiving an HttpResponseMessage
, but when I try to read it the Content Stream is always null
. I suspect it is something I am doing wrong on the server side though.
Server side code:
[MobileAppController,Authorize,RoutePrefix("api/SomeController")]
public class someController : ApController{
[HttpPost,Route("theRoute/{id}"),AllowAnonymous]
public HttpResponseMessage someFunction(args)
{
return new HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
Content = new StringContent("Hello");
}
}
}
If I instead display the response with res.ToString()
, I get a response message with
StatusCode:200
ReasonPhrase:'OK'
Version:1.1
Content: System.Net.HttpStreamContent
Headers:{
Server: Microsoft-IIS/10.0
X-Powered-By:ASP.NET,
Date: Tue,20,Feb 2018 17:08:42 GMT,
Content-Length:4
Content-Type: application/json; charset=utf-8
}
I've been trying to figure out why Content
is null
but I can't figure out why.
Edit 1: App.Client is the HttpClient
used by the entire mobile application.
In my app.xaml.cs
file:
static HttpClient client;
public static HttpClient Client
{
get
{
if (client == null)
{
client = new HttpClient();
}
return client;
}
}