public static async Task <IActionResult> POST_noDB( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "postroute")] HttpRequest req, ILogger log) { //Converting body to ExampleRequest string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); ExampleRequest drinkRequest = JsonConvert.DeserializeObject <ExampleRequest>(requestBody); //Do stuff with your ExampleRequest instance. return(new OkObjectResult("Cool_Data")); }
public static async Task <IActionResult> Post_DB( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "postroute")] HttpRequest req, ILogger log) { //Converting body to ExampleRequest string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); ExampleRequest exampleRequest = JsonConvert.DeserializeObject <ExampleRequest>(requestBody); /* * * Dont forget to add your ConnectionString to local.settings.json and changee the name under here * Can be found in the Azure portal * */ //Database shit string connectionString = Environment.GetEnvironmentVariable("CHANGE_ME"); using (SqlConnection sqlConnection = new SqlConnection(connectionString)) { await sqlConnection.OpenAsync(); using (SqlCommand command = new SqlCommand()) { command.Connection = sqlConnection; //Your SQL query command.CommandText = $"INSERT INTO Bezoekers Values (@Name, @Address, @Age)"; // Add parameters command.Parameters.AddWithValue("@Name", exampleRequest.name); command.Parameters.AddWithValue("@Address", exampleRequest.address); command.Parameters.AddWithValue("@Age", exampleRequest.age); //execute await command.ExecuteNonQueryAsync(); //return object to API return(new OkObjectResult(exampleRequest)); } } //Do stuff with your ExampleRequest instance. }
public static async Task <IActionResult> PostAzureStorage( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "postroute")] HttpRequest req, ILogger log) { //Converting body to ExampleRequest string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); ExampleRequest exampleRequest = JsonConvert.DeserializeObject <ExampleRequest>(requestBody); //Converting ExampleRequest to ExampleRequestEntity for use in Azure Table Storage ExampleRequestEntity exampleRequesEntity = new ExampleRequestEntity() { name = exampleRequest.name, address = exampleRequest.address, age = exampleRequest.age }; /* * * Dont forget to add your ConnectionString to local.settings.json and change the name under here * Can be found in the Azure portal * */ string connectionString = Environment.GetEnvironmentVariable("CHANGE_ME"); //Azure Table storage shit CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(connectionString); CloudTableClient cloudTableClient = cloudStorageAccount.CreateCloudTableClient(); CloudTable cloudTable = cloudTableClient.GetTableReference("registrations"); // Creates table if not exists await cloudTable.CreateIfNotExistsAsync(); // insert entity into table TableOperation insertOperation = TableOperation.Insert(exampleRequesEntity); // execute insert operation await cloudTable.ExecuteAsync(insertOperation); return(new OkObjectResult(exampleRequesEntity)); }