Search code examples
c#restapirefit

How can I pass a list of strings within a request?


I'm using Refit library for my app and I need to make a call to another service. I need to get all entities with ids that I'm passing.

I tried [Body] attribute and it still doesn't work. I manage to pass a request but the list if ids that another service gets is null while I'm definitely passing existing IEnumerable.

My IRefitProxy:

[Get("/students/allByIds")]
Task<IEnumerable<Student>> GetStudentsById(IEnumerable<string> ids);

Another service's API:

[RoutePrefix("api/students")]
[Route("allByIds")]
[HttpGet]
public IEnumerable<Student> AllByIds(IEnumerable<string> ids)
{
//ids here is null!

//call my repository blablabla
return students;
}

I pass an array/List of strings and it comes as null. The path is ok because I manage to fall into the method with breakpoint. How can I manage to pass it correctly?


Solution

  • I managed to solve this question. Adding [Query(CollectionFormat.Multi)] solved the problem.

    [Get("/students/allByIds")] Task<IEnumerable<Student>>GetStudentsById([Query(CollectionFormat.Multi)]IEnumerable<string> ids);

    The receiving API needs to have [FromUri] attribute. Hope it helps someone!