user7374044
user7374044

Reputation: 167

Postman gets data from api but .net core 2.2 app not

I can't retreive response from backend API in .NET CORE 2.2 app. but in Postman

I can retreive data. In .NET CORE 2.2 app throw an error:

An unhandled exception occurred while processing the request.
IOException: The server returned an invalid or unrecognized response.
System.Net.Http.HttpConnection.FillAsync()

HttpRequestException: Error while copying content to a stream.
System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer)

HttpRequestException: Error while copying content to a stream.
System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer)
System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task<HttpResponseMessage> sendTask, HttpRequestMessage request, CancellationTokenSource cts, bool disposeCts)
Test.Controllers.HomeController.Login(AuthUser authUser) in HomeController.cs
+
            var response = await client.PostAsync("http://192.168.43.96:9890/api/login", content);
Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor+TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
System.Threading.Tasks.ValueTask<TResult>.get_Result()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

I've tried to change connection value to close, but it didn't work out.

In C# I have following code:

string response = await "http://192.168.43.96:9890/api/login".PostJsonAsync(new { login = "admin", password = "admin" }).ReceiveJson();

I do the request in C# with fluent HTTP. I've tested httpClient and it didn't work out.

Backend is written in C. The response which produce backend is below:

HTTP/1.1 200 OK
Content-Length: 262
Connection: keep-alive
Content-Type: application/json
Server: Simple HTTP with C server
Date: Sun Jun  9 19:03:06 2019


{
    "data": [{
            "id":   "1",
            "login":    "admin",
            "first_name":   "Main",
            "last_name":    "Admin",
            "email":    "[email protected]",
            "role_id":  "1"
        }],
    "token":    {
        "value":    "c509fe9566db8302ef11d78974579bc9a825d617c44d78bfeda959d3a8d9f163"
    }
}

Finally I want to make it happen in this .NET CORE 2.2 APP.

Please help.

Upvotes: 0

Views: 509

Answers (1)

Nan Yu
Nan Yu

Reputation: 27588

You could try to create the entities in your client side like :

public class Datum
{
    public string id { get; set; }
    public string login { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string email { get; set; }
    public string role_id { get; set; }
}

public class Token
{
    public string value { get; set; }
}

public class RootObject
{
    public List<Datum> data { get; set; }
    public Token token { get; set; }
}

And use HttpClient to send request to your server side , read response and deserialize Object :

try
{
    using (HttpClient client = new HttpClient())
    {
        var content = new StringContent(jsonInString, Encoding.UTF8, "application/json");
        var response = await client.PostAsync(url, content);
        if (response != null)
        {
            var jsonString = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<RootObject>(jsonString);
        }
    }
}
catch (Exception ex)
{

}

Upvotes: 1

Related Questions