Connection to API using FrameWork 4.8

Fábio Freitas 0 Reputation points

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: 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.
259 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce ( 48,976 Reputation points

    see docs:

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

    0 comments No comments

  2. Lan Huang-MSFT 20,136 Reputation points Microsoft Vendor

    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.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("Internal server Error");

    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