C
C#16mo ago
DayanK

❔ Post Request no working

please I tried to make a post request but I receive some error. How I can fix it ? I'm using .net core 6 version. Thanks in advance public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } } }
7 Replies
jiniux
jiniux16mo ago
read the error
Pobiega
Pobiega16mo ago
You'll need to post the code for the function that is throwing the error, if you actually want help but yeah, its "Microsoft.Data.SqlClient" that is throwing the error, that tells me a lot
DayanK
DayanKOP16mo ago
cs using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace ServerLessRestApi
{
public class ProductsGetAllCreate
{
private readonly AppDbContext _context;

public ProductsGetAllCreate (AppDbContext context)
{
_context = context;
}

[FunctionName("ProductsGetAllCreate")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "products")] HttpRequest req)
{
if(req.Method == HttpMethods.Post)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var product = JsonConvert.DeserializeObject<Product>(requestBody);

_context.Products.Add(product);
await _context.SaveChangesAsync();
return new CreatedResult("/products", product);
}

var products = await _context.Products.ToListAsync();
return new OkObjectResult(products);
}
}
}
cs using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace ServerLessRestApi
{
public class ProductsGetAllCreate
{
private readonly AppDbContext _context;

public ProductsGetAllCreate (AppDbContext context)
{
_context = context;
}

[FunctionName("ProductsGetAllCreate")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "products")] HttpRequest req)
{
if(req.Method == HttpMethods.Post)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var product = JsonConvert.DeserializeObject<Product>(requestBody);

_context.Products.Add(product);
await _context.SaveChangesAsync();
return new CreatedResult("/products", product);
}

var products = await _context.Products.ToListAsync();
return new OkObjectResult(products);
}
}
}
Pobiega
Pobiega16mo ago
$code
MODiX
MODiX16mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Pobiega
Pobiega16mo ago
also, the problem seems to lie in the setup of your context or the context itself
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server