4

I'm trying to get Flurl to work and I'm stuck on how gets work when passing in an ID.

[HttpGet("Get/{id}")]
public IActionResult Get(int id)
{
    // Some something and return
}

The above expects

Get/1

So in Flurl:

var result = await _baseUrl
    .AppendPathSegment("/Get")
    .SetQueryParam("id", id)
    .GetJsonAsync();

This produces this:

/Get?id=8

...which then fails with a 404.

How can I get Flurl to set a query param that is /id or get my get to accept both Get/id and Get?id=

I can do the following but it doesn't seem very elegant

var result = await _baseUrl
    .AppendPathSegment(string.Format("/Get/{0}", id))
    .GetJsonAsync();
DavidG
  • 113,891
  • 12
  • 217
  • 223
Michael Esteves
  • 1,445
  • 3
  • 21
  • 38
  • 1
    I've not used Flurl, but I am guessing that `SetQueryParam` will set a querystring, but you are building a path. Can you do `_baseUrl.AppendPathSegments("get", id.ToString())`? – DavidG Aug 02 '17 at 12:48
  • @DavidG I can, no need for the .ToString. That was simple! Add an answer and I'll accept. Thanks – Michael Esteves Aug 02 '17 at 12:52

2 Answers2

3

SetQueryParam will add the value as a query string but you need the value to be part of the path. Instead consider using the AppendPathSegments method, for example:

var result = _baseUrl
    .AppendPathSegments("get", id)
    .GetJsonAsync();
DavidG
  • 113,891
  • 12
  • 217
  • 223
1

I use it like this:

var result = "https://api.site.com/v1/".AppendPathSegment("endpoint").AppendPathSeparator().SetQueryParam("get", id)
// Outputs: "https://api.site.com/v1/endpoint/?get=5"
Chandra Kumar
  • 4,127
  • 1
  • 17
  • 25