{ return $"{{\"error\":\"0\", \"message\":\"\", \"rows\":\"0\"," /> { return $"{{\"error\":\"0\", \"message\":\"\", \"rows\":\"0\"," /> { return $"{{\"error\":\"0\", \"message\":\"\", \"rows\":\"0\","/>

postAsync returns status as 200, ok but response.content is empty while data is returned in postman

1k views Asked by At

I am using .net 6.0 minimal api with the below simple code in c#

app.MapGet("/", () =>
{
    return $"{{\"error\":\"0\", \"message\":\"\", \"rows\":\"0\", \"data\":\"It is working !\"}}";
});

My client side code in c# is as below

using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
//
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri($"{(Properties.Settings.Default.use_https ? "https://" : "http://")}{Properties.Settings.Default.server_address}:Properties.Settings.Default.server_port}/");
HttpResponseMessage response = await httpClient.GetAsync("");

the response.Content is

{System.Net.Http.StreamContent}
    Headers: {Content-Type: text/plain; charset=utf-8
}
    IsBuffered: true
    bufferSize: 4096
    bufferedContent: {System.Net.Http.HttpContent.LimitMemoryStream}
    canCalculateLength: false
    content: {System.Net.Http.HttpClientHandler.WebExceptionWrapperStream}
    contentConsumed: true
    contentReadStream: null
    disposed: false
    headers: {Content-Type: text/plain; charset=utf-8
}
    start: 0

With postman I am getting a proper result i.e. {"error":"0", "message":"", "rows":"0", "data":"It is working !"} from get of http://localhost:5400/

Maybe it is something to do with 'content: {System.Net.Http.HttpClientHandler.WebExceptionWrapperStream}'? How do I resolve this?

1

There are 1 answers

0
hsn-mnj On

You are calling it using await GetAsync() method, try to convert your lambda to an async method like this:

app.MapGet("/",async Task<string> () =>
{
    return await Task.FromResult("Your response");
});