GET and POST functioning

This commit is contained in:
2024-06-05 11:34:55 +02:00
parent 004e9c87f9
commit c6db3d426b
5 changed files with 140 additions and 37 deletions

View File

@ -1,4 +1,5 @@
using System.Text.Json;
using System.Text;
using System.Text.Json;
namespace PostmanCloneLibrary;
@ -6,13 +7,42 @@ public class ApiAccess : IApiAccess
{
private readonly HttpClient _httpClient = new();
public async Task<string> CallApiAsync(
string url,
bool formaOutput = true,
HttpAction action = HttpAction.GET
string content,
HttpAction action = HttpAction.GET,
bool formaOutput = true
)
{
StringContent stringContent = new(content, Encoding.UTF8, "application/json");
return await CallApiAsync(url, stringContent, action, formaOutput);
}
public async Task<string> CallApiAsync(
string url,
HttpContent? content = null,
HttpAction action = HttpAction.GET,
bool formaOutput = true
)
{
var response = await _httpClient.GetAsync(url);
HttpResponseMessage response;
switch (action)
{
case HttpAction.GET:
response = await _httpClient.GetAsync(url);
break;
case HttpAction.POST:
response = await _httpClient.PostAsync(url, content);
break;
default:
throw new ArgumentOutOfRangeException(nameof(action), action, null);
}
//response = await _httpClient.GetAsync(url);
if (response.IsSuccessStatusCode)
{
string json = await response.Content.ReadAsStringAsync();