Using Fast-Endpoints how can I set the location header to use path params instead of query params in a 201 response?

927 views Asked by At

Looking at the documentation I only found this usage example:

await SendCreatedAtAsync<GetUser.Endpoint>(new { id = response.Id}, response);

This produces a 201 response and the location header is set to something like:

"api/foo/bars?id="12345"

I would like that the location header was something like this:

"api/foo/bars/12345"

How can I achieve that?

1

There are 1 answers

0
Dĵ ΝιΓΞΗΛψΚ On BEST ANSWER

i'm unable to reproduce your issue with the following:

User Create Endpoint

public class Endpoint : EndpointWithoutRequest
{
    public override void Configure()
    {
        Post("api/user/create");
        AllowAnonymous();
    }

    public override async Task HandleAsync(CancellationToken c)
    {
        var response = "user created!";
        await SendCreatedAtAsync<GetUserEndpoint>(new { id = 101 }, response);
    }
}

User Get Endpoint

public class GetUserEndpoint : EndpointWithoutRequest<GetUserResponse>
{
    public override void Configure()
    {
        Get("/api/user/{id}");
        AllowAnonymous();
    }

    public override Task HandleAsync(CancellationToken ct)
    {
        return SendAsync(new()
        {
            Id = Route<int>("id"),
            Name = "Test"
        });
    }
}

i see the correct url in the response header like so:

enter image description here

i'm assuming your anonymous object which is passed in to SendCreatedAtAsync might not be correctly matching with the route param of the GetUserEndpoint.

if that's not the case, show me how to replicate the problem.