see docs:
note: the 4.8 framework was designed around soap and xml, but has added json support.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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?
see docs:
note: the 4.8 framework was designed around soap and xml, but has added json support.
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.
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");
}
}
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.