Connection to API using FrameWork 4.8

Fábio Freitas 0 Reputation points
2023-11-06T13:35:03+00:00

Good morning,

I'm trying to connect to an API, but the example provided uses .net 6.0, but my project is using FrameWork 4.8, for example, I don't have the Text.Json library and I believe I'll need another one to get the answer.

  request.Content = new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/json"); //CONTENT-TYPE header
         Task<HttpResponseMessage> response = client.SendAsync(request);
         return await response.Result.Content.ReadFromJsonAsync<Token>(OpcoesDesserializarJson);
private static JsonSerializerOptions OpcoesDesserializarJson
     => new() { PropertyNameCaseInsensitive = true };

Any suggestion?

ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
343 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,706 Reputation points
    2023-11-06T19:36:09.5266667+00:00

    see docs:

    https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client

    note: the 4.8 framework was designed around soap and xml, but has added json support.

    0 comments No comments

  2. Lan Huang-MSFT 29,751 Reputation points Microsoft Vendor
    2023-11-08T08:06:13.24+00:00

    Hi @Fábio Freitas,

    I don't have the Text.Json library and I believe I'll need another one to get the answer.

    You can use NuGet Package Manager to install the Web API Client Libraries package.

    • Microsoft.AspNet.WebApi.Client
    • Newtonsoft.Json

    Refer to the code below:

    RequestWorkorder p = new RequestWorkorder()
    {
        Program = "4",
        Workorder = "D"
    };
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("https://localhost:****/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        //GET Method
        HttpResponseMessage response = await  client.GetAsync("api/Values");
        if (response.IsSuccessStatusCode)
        {
            List<RequestWorkorder> requestWorkorder = null;
            requestWorkorder = await response.Content.ReadAsAsync<List<RequestWorkorder>>();
            Console.WriteLine("Id:{0}\tName:{1}", requestWorkorder[0].Program, requestWorkorder[0].Workorder);
    
        }
        //POST Method
        HttpResponseMessage response1 = await client.PostAsJsonAsync("api/Values", p);
    
        if (response1.IsSuccessStatusCode)
        {
    
           
            Console.WriteLine("Success");
        }
        else
        {
            Console.WriteLine("Internal server Error");
        }
    }
    

    https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.