As the title of this question tells, how could I do a GET request with Fast Endpoints in ASP.NET
Now I have tried to do this:
using api.Blogs.Shared;
using api.Data;
using AutoMapper;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace api.Blogs.ListBlogsEndpoint;
public class ListBlogsEndpoint(BlogDbContext context, AutoMapper.IMapper mapper) : Endpoint<ListBlogsRequest, BlogResponse[]>
{
public override void Configure()
{
Get("blogs");
}
public override async Task HandleAsync(ListBlogsRequest req, CancellationToken ct)
{
var blogs = await context.Blogs.ToArrayAsync(ct);
var response = blogs.Select(b => mapper.Map<BlogResponse>(b)).ToArray();
await SendOkAsync(response, ct);
}
}
But when I try to do a get request I get the following error:
TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
I don't know what to do now, I have searched about it, and it seems that the Request in a GET request should be query parameters, but they are used here as a body
EDIT: I'm using swagger, and it provide me with a body, so I think if I use postman it would be fine.
most likely you have gotten the middleware setup wrong. the following works out of the box:
var bld = WebApplication.CreateBuilder(args);
bld.Services
.AddFastEndpoints()
.SwaggerDocument(); //define swagger doc
var app = bld.Build();
app.UseFastEndpoints()
.UseSwaggerGen(); //enable swagger middleware
app.Run();
public record ListBlogsRequest(string? Query);
sealed class ListBlogsEndpoint : Endpoint<ListBlogsRequest, string[]>
{
public override void Configure()
{
Get("blogs");
AllowAnonymous();
}
public override async Task HandleAsync(ListBlogsRequest req, CancellationToken ct)
{
string[] response = ["one", "two", req.Query!];
await SendAsync(response);
}
}
you may have called swagger methods that use swashbuckle (now abandoned), which is not supported by fastendpoints. do the swagger middleware as explained in the docs here.