4

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;
    }
}
Dai
  • 141,631
  • 28
  • 261
  • 374
user3221924
  • 41
  • 1
  • 1
  • 4
  • Is `App.Client.PostAsync` a method you have created? If yes, you might be disposing of the client before you get the chance to read the stream.# – npinti Feb 20 '18 at 17:20
  • Have you tried simply returning a string or an object from your controller? – Sparsha Bhattarai Feb 20 '18 at 17:23
  • 1
    Which is null actually? res.Content? Did you checked whether the url is correct or not and try to test it through postman(chrome extension) to find your backend is working fine. If it is working then just debug and check what res is having. If res.Content is there and if s is having a value then i guess you are missing **s.Result** to get its result while displaying alert message. Just check this link as well **https://stackoverflow.com/questions/12413287/how-do-i-extract-content-from-httpresponsemessage-from-post-when-using-web-api** – Sivaprasath Feb 20 '18 at 17:40
  • returning a string or object works fine with GET requests, but I can't seem to get POST requests to work right. – user3221924 Feb 20 '18 at 17:41
  • try setting the content type explicitly like Content = new StringContent("Hello", Encoding.UTF8, "text/plain"); – Aman B Feb 20 '18 at 17:55
  • Explicitly setting content type is still giving me null from Content.ReadAsStringAsync() – user3221924 Feb 20 '18 at 18:03
  • could you share the code from the API side? I think problem lies there. – chaosifier Feb 23 '18 at 10:17

1 Answers1

-1

Try to set the client DefaultRequestHeaders on the application response type. For example:

    client.DefaultRequestHeaders.Add("Accept", "application/json");
doxsi
  • 1,002
  • 19
  • 42