refit icon indicating copy to clipboard operation
refit copied to clipboard

String formatting for URL segments

Open sadedil opened this issue 2 years ago • 1 comments

First I wanted to say, this is a great time-saver library. Thank you. I ❤ Refit.

Is your feature request related to a problem? Please describe. I need to send a GET query to an endpoint with a GUID including curly braces like /api/orders/123/files/{01cdce89-9ba8-450b-96fa-4faf97eee4c0} and I'm using a complex object as a request parameter.

My method looks like this:

[Get("/api/orders/{request.OrderId}/files/{request.FileId}")]
Task<IApiResponse<string>> GetFileUrl(GetFileUrlRequest request);

My main problem is, I can't add curly braces in URL segment.

Describe the solution you'd like

  • First solution could be using escape characters for { and }, so I could write my attribute like this [Get(@"/api/orders/{request.OrderId}/clientfiles/\{{request.FileId}\}")] For now it's not supported (I suppose)

  • Second solution, like [Query (Format = "something")], I would like to be able to format URL segments [Segment (Format = "something")] For now it's not supported (I suppose)

Describe suggestions on how to achieve the feature For now I've modified my request POCO like this:

public class GetFileUrlRequest
{
    [JsonIgnore]
    public int OrderId { get; set; }

    [JsonIgnore]
    public Guid FileId { private get; set; }

    [JsonIgnore]
    public string FileIdWithCurlyBraces => $"{FileId:B}";
}

And my get attribute looks like [Get("/api/orders/{request.OrderId}/files/{request.FileIdWithCurlyBraces}")]

I don't like this method because of two reasons:

  • First reason: I have to add private get to FileId property, otherwise it's adding this property to the end of the query as a query parameter like this: /api/orders/123/files/{01cdce89-9ba8-450b-96fa-4faf97eee4c0}?FileId=01cdce89-9ba8-450b-96fa-4faf97eee4c0}. Maybe Refit has some another attribute to ignore a field without using a private set but I couldn't find.

  • Second reason: I need to add another field like FileIdWithCurlyBraces for this to achieve simple formatting.

Best, Mustafa

sadedil avatar Feb 04 '22 11:02 sadedil

URL encode the desired { and } into %7B and %7D

[Get("/api/orders/{request.OrderId}/files/%7B{request.FileId}%7D")]

mrmoses avatar Feb 05 '24 21:02 mrmoses