I have been working Flutter app with .NET Core web API to connect the database. Now I have a question about a Web API.
In my backend activity through web API, I need to call an external API endpoint to make some changes in the external app and I’m using .net core web API also enabled swagger and i will implemented below code and tested it not calling external Api and nothing changed in external app Client Side
This is my code
namespace test.Controllers
{
public class testController : ControllerBase
{
private ItestService _tstService;
public TestController(ItestService tstService)
{
_tstService = tstsService;
}
[HttpPost]
[Produces("application/json")]
[ProducesResponseType(200, Type = typeof(string))]
[Route("updatemyexternalapp")]
[ValidateHeader]
[SwaggerHeader("x-api-key", "Client ID required - (eg:string)", "", true)]
public IActionResult UpdateexternalApi([FromBody] UpdateexternalApiRequest model, [FromHeader(Name = "x-api-key")] string kid = null)
{
if (model is null) return BadRequest("Request form body empty - Please try again!");
var response = _tstService.updateexternalApi(model);
return Ok(response);
}
}
namespace Services.Services
{
public interface ItestService
{
string updateexternalApi(UpdateexternalApiRequest model);
}
public class TestService : ItestService
{
public string updateexternalApi(UpdateexternalApiRequest model){
do some activity related in my database
Task.Factory.StartNew(() =>ApiToExternalAPI());
return 'saved successfully';
}
public async Task<bool> ApiToExternalAPI()
{
using (var client = new HttpClient())
{
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
string ServiceUrl = 'url';
string ServiceKey = 'key';
client.BaseAddress = new Uri(ServiceUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("x-api-key", ServiceKey);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
client.DefaultRequestHeaders.Accept.Add(new var response = await client.PostAsync(apiPath, formData).ConfigureAwait(false);
var responseContent = await response.Content.ReadAsStringAsync();
var value = false;
if (response.IsSuccessStatusCode)
{
value = true;
}
else
{
value=false;
}
return value;
}
}
}