2

Hello guys

I'am currently working in Xamarin and got stucked with basic operation. Iam trying to call my NetCore web API from Xamarin using Refit.

The main problem is - I guess - that i want to pass object in paramater of call. The second problem could be that object has a List of enums as property.

Models:

public class ArticleFilter
{
    public List<ArticleType> Types { get; set; }

    public int? CarBrandId { get; set; }
}

public enum ArticleType
{
    News = 1,
    SpecialOffer = 2,
    Service = 3,
}

Refit Interface:

[Headers("Content-Type: application/json")]
public interface IArticleApi
{        
    [Get("/api/articles")]
    [Headers("Content - Type: application / json")]
    Task<List<Article>> GetAll(ArticleFilter filter);

    [Get("/api/articles/{id}")]
    Task<ArticleDetail> GetOne(int id);
}

Api controller:

    [HttpGet]
    public async Task<IActionResult> GetAll([FromUri] ArticleFilterDto filter)
    {
        var articles = await _articleSectionService.GetAll(filter);

        if (articles == null)
        {
            return NotFound();
        }                

        return Ok(articles);
    }

Postman call: enter image description here

Test calls from Xamarin view model:

var filter = new ArticleFilter()
{
    Types = new List<ArticleType> { ArticleType.News }
};
               
var test = new List<Article>();
test = await response.GetAll(filter);

Actually this one falls in exception - 415 - Unsupported media type

I came across their GitHub documentation but still no progress. Could some Superman help me please?

parysoid
  • 87
  • 8

1 Answers1

3

Finally solved

Just in case anyone ever will be in same situation. In my case changing from [Body]/[FromUri] to [Query] in Interface and [FromQuery] in controller method solved my problem.

[Headers("Content-Type: application/json")]
public interface IArticleApi
{
    [Get("/api/articles/")]       
    Task<List<Article>> GetAll([Query]ArticleFilterDto filter);
}

[HttpGet]
public async Task<IActionResult> GetAll([FromQuery]ArticleFilterDto filter)
{
   var articles = await _articleSectionService.GetAll(filter);

   if (articles == null)
   {
       return NotFound();
   }                

   return Ok(articles);
}
parysoid
  • 87
  • 8