0

I have to write an azure function using a time trigger that will hit an API every weekend and fetch the data from that API and store that data to my Azure SQL database.

So, I am not getting how to call an API from time trigger azure function to fetch data and how to store that data into my azure SQL database.

  • can you line out what you've tried already ? – monofone Mar 14 '20 at 23:05
  • I have tried HTTPCLIENT to call API in my Azure function and then after taking the response of HTTPClient I tried to store that JSON format data into my local database. But it's not working. Actually I am not getting how to do both binding together in azure function 1st to call an API to fetch data and 2nd take that data and store that data into my local database or in azure sql database, How to do these thing together in Time trigger Azure function. – Puneet Gahlot Mar 16 '20 at 02:58
  • You haven't posted any code, or an error message. All of the information you need is on the internet. Please try something and ask a question when you run into problems – Nick.Mc Mar 21 '20 at 13:08

1 Answers1

1

You can follow this link to get started Azure Function timer trigger.

You have to use HTTPClient to call Api.

static HttpClient client = new HttpClient();
// Update port # in the following line.
client.BaseAddress = new Uri("http://localhost:64195/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/json"));

Product product = null;
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
    product = await response.Content.ReadAsAsync<Product>();
}
return product;

Note: If you have to call lots of API's/endpoints, you may get port exhaustion error. HttpClientFactory is recommended for that scenario.

Pankaj Rawat
  • 4,037
  • 6
  • 41
  • 73