GET and POST functioning
This commit is contained in:
@ -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();
|
||||
|
||||
Reference in New Issue
Block a user