Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.2 kB
3
Indexable
Never
protected async Task<string> SendPostApi(string url,
                                        string jsonData,
                                        HttpClient httpClient = null)
{
    try
    {
        string resultado = null;
        var clientePost = new HttpClient();
        clientePost.DefaultRequestHeaders.Add("Accept", "*/*");

        if (httpClient != null) clientePost = httpClient;

        StringContent httpContent = new StringContent(jsonData, System.Text.Encoding.UTF8, "application/json");
        HttpResponseMessage responseApi = await clientePost.PostAsync(url, httpContent);

        if (responseApi.IsSuccessStatusCode)
        {
            Task<string> jsonResponse = responseApi.Content.ReadAsStringAsync();
            resultado = jsonResponse.Result;
        }
        else
        {
            Notificar(responseApi.Content.ReadAsStringAsync().Result);
        }

        if (string.IsNullOrEmpty(resultado))
        {
            return null;
        }

        return resultado;
    }
    catch (Exception ex)
    {
        Notificar(ex, "Ocorreu um erro durante a comunicação com a api", _logger);
        throw;
    }
}
Leave a Comment