C
C#2mo ago
James!!

im not getting any response on these endpoints

var builder = WebApplication.CreateBuilder(args);

// Add services to the container
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});

// Register services
builder.Services.AddHttpClient("OpenAI", client =>
{
client.BaseAddress = new Uri("https://api.openai.com/v1/");
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {builder.Configuration["OpenAI:ApiKey"]}");
});
builder.Services.AddScoped<PdfService>();
builder.Services.AddScoped<ContractAnalysisService>();
builder.Services.AddScoped<DatabaseService>();

var app = builder.Build();

// Initialize database
using (var scope = app.Services.CreateScope())
{
var dbService = scope.ServiceProvider.GetRequiredService<DatabaseService>();
dbService.InitializeDatabase();
}

// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseCors("AllowAll");
app.UseHttpsRedirection();
var builder = WebApplication.CreateBuilder(args);

// Add services to the container
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});

// Register services
builder.Services.AddHttpClient("OpenAI", client =>
{
client.BaseAddress = new Uri("https://api.openai.com/v1/");
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {builder.Configuration["OpenAI:ApiKey"]}");
});
builder.Services.AddScoped<PdfService>();
builder.Services.AddScoped<ContractAnalysisService>();
builder.Services.AddScoped<DatabaseService>();

var app = builder.Build();

// Initialize database
using (var scope = app.Services.CreateScope())
{
var dbService = scope.ServiceProvider.GetRequiredService<DatabaseService>();
dbService.InitializeDatabase();
}

// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseCors("AllowAll");
app.UseHttpsRedirection();
12 Replies
James!!
James!!OP2mo ago
// Endpoints
app.MapPost("/api/contracts/upload", async (
HttpContext context,
PdfService pdfService,
ContractAnalysisService analysisService,
DatabaseService dbService) =>
{
try
{
var file = context.Request.Form.Files[0];
if (file == null || file.Length == 0)
return Results.BadRequest("No file uploaded");

var text = await pdfService.ExtractTextFromPdf(file);
var contractInfo = await analysisService.AnalyzeContract(text);
await dbService.SaveContract(contractInfo);

return Results.Ok(contractInfo);
}
catch (Exception ex)
{
return Results.Problem(ex.Message);
}
});

app.MapGet("/api/contracts/{id}", async (
string id,
DatabaseService dbService) =>
{
try
{
var contract = await dbService.GetContract(id);
return contract != null ? Results.Ok(contract) : Results.NotFound();
}
catch (Exception ex)
{
return Results.Problem(ex.Message);
}
});

app.Run();
// Endpoints
app.MapPost("/api/contracts/upload", async (
HttpContext context,
PdfService pdfService,
ContractAnalysisService analysisService,
DatabaseService dbService) =>
{
try
{
var file = context.Request.Form.Files[0];
if (file == null || file.Length == 0)
return Results.BadRequest("No file uploaded");

var text = await pdfService.ExtractTextFromPdf(file);
var contractInfo = await analysisService.AnalyzeContract(text);
await dbService.SaveContract(contractInfo);

return Results.Ok(contractInfo);
}
catch (Exception ex)
{
return Results.Problem(ex.Message);
}
});

app.MapGet("/api/contracts/{id}", async (
string id,
DatabaseService dbService) =>
{
try
{
var contract = await dbService.GetContract(id);
return contract != null ? Results.Ok(contract) : Results.NotFound();
}
catch (Exception ex)
{
return Results.Problem(ex.Message);
}
});

app.Run();
Keswiik
Keswiik2mo ago
have you set breakpoints to see if those handlers are being called when you make a request to that route?
James!!
James!!OP2mo ago
yep i did i also tested with a basic endpoint as well currently im struggling with the /api/contracts/upload endpoint
Keswiik
Keswiik2mo ago
what does your request look like? and do both endpoints fail or only the post?
James!!
James!!OP2mo ago
currently the post request, not sure about the get request yet cuz it will be emplty anyways
Keswiik
Keswiik2mo ago
going by that error message, your issue is not passing along a Content-Type header in your request
Sehra
Sehra2mo ago
maybe you need to take an IFormFile to make swagger happy
Keswiik
Keswiik2mo ago
if you wrote that curl command by hand, add -H "Content-Type: application/x-www-form-urlencoded" or -H "Content-Type: multipart/form-data"
James!!
James!!OP2mo ago
naah bro, that's auto generated by the browser ig
Keswiik
Keswiik2mo ago
then run it in your CLI somewhere to test and see if that is the issue easiest way to validate
James!!
James!!OP2mo ago
yep, i will do good idea! @Sehra @Keswiik Ig I have got the problem Now I have to fix it Have to add iformfile, then turn of anti-forgery and then have to configure a startup.cs

Did you find this page helpful?