code : API to call external api to read and that returned value should write into local server database table
eg:from external api we will get id and name value it should write into id and name of employee table
code : API to call external api to read and that returned value should write into local server database table
eg:from external api we will get id and name value it should write into id and name of employee table
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Mar 15, 2023, 12:17 AM EDT
You can use the HttpClient class or you can use RestSharp.
Using HttpClient:
using (var client = new HttpClient()) { var request = new HttpRequestMessage(HttpMethod.Get, apiUrl); var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { var responseBody = await response.Content.ReadAsStringAsync(); } else { throw new Exception($"Failed to call remote API. Status code: {response.StatusCode}"); } }C#
Using RestSharp:
using RestSharp; public void CallRemoteApi(string apiUrl) { var client = new RestClient(apiUrl); var request = new RestRequest(Method.GET); IRestResponse response = client.Execute(request); if (response.IsSuccessful) { var responseBody = response.Content; } else { throw new Exception($"Failed to call remote API. Status code: {response.StatusCode}"); } }Tuhin PaulRank #7 · 71.9K reputationPosted Mar 14, 2023, 9:10 PM EDT
To call an external API and write its returned values to a local database table using C#, check the steps below:
Step1: Create a C# project in your preferred development environment.
Step2: Add the necessary dependencies to your project to make HTTP requests to the external API and to access your local database. For example, you can use the HttpClient class to make HTTP requests and the Entity Framework library to access your local database.
Step3: Define a model class to represent the data returned by the external API. For example, you can define a class with properties for the "id" and "name" values.
Step4: Define a model class to represent the data stored in your local database table. For example, you can define an Employee class with properties for the "id" and "name" fields.
Step5: Create an instance of the HttpClient class and use it to make a request to the external API. Parse the response to extract the relevant data and create an instance of your model class representing the returned data.
Step6: Use Entity Framework or another data access library to connect to your local database and create a new Employee object with the relevant data from the external API response.
Step7: Save the Employee object to your local database.
sample code to get you started:
using System; using System.Net.Http; using System.Threading.Tasks; using Newtonsoft.Json; namespace ExternalApiDemo { class Program { static async Task Main(string[] args) { // 1. Create a C# project and add necessary dependencies // 2. Define models for external API response and local database table class ExternalApiResponse { public int Id { get; set; } public string Name { get; set; } } class Employee { public int Id { get; set; } public string Name { get; set; } } // 3. Make HTTP request to external API and parse response var httpClient = new HttpClient(); var response = await httpClient.GetAsync("https://example.com/api/employees"); var responseContent = await response.Content.ReadAsStringAsync(); var apiResponse = JsonConvert.DeserializeObject(responseContent); // 4. Write API response to local database using (var db = new MyDatabaseContext()) { var employee = new Employee { Id = apiResponse.Id, Name = apiResponse.Name }; db.Employees.Add(employee); db.SaveChanges(); } } } }In the example code above, MyDatabaseContext is a class representing your local database and Employees is a property on that class representing your "employee" table.