3

This is my API method and it takes 3 parameters from body


    public async Task<IEnumerable<EnCurso>> GetIdCondByDTRuta(EnCurso encurso)
            {
                var db = dbConnection();
                return await db.QueryAsync<EnCurso>("select * from public.tb_encurso where to_timestamp('" + encurso.inicio+ "','DD/MM/YYYY HH24:MI:SS') between inicio AND fin and idruta = " + encurso.idruta+ " and idbus = "+ encurso.idbus + " and estado=true;", new { encurso.inicio, encurso.idruta, encurso.idbus });
            }

    [HttpGet("GetIdCondByDTRuta")]
            public async Task<IActionResult> GetIdCondByDTRuta([FromBody] EnCurso encurso)
            {
                return Ok(await _encursoRepository.GetIdCondByDTRuta(encurso));
            }

When testing on Postman from body works fine. Postman test

But then I don't know how to send content from the app consuming the API. I tried adding the parameters in the URI, like this


    var _URI = "http://XXX.XXX.0.XX:4XXX8/api/encurso/GetIdCondByDTRuta.json?inicio:" + encurso.inicio + "&idruta:" + encurso.idruta + "&idbus:" + encurso.idbus;
                HttpResponseMessage result = await client.GetAsync(_URI);

Doesn't work. I tried search for a httpclient method that takes the Uri and content, as the post do, but GET methods don't have the option

Post content example

  • 1
    GET methods shouldn't accept body parameters. It would make more sense to modify your server method to accept three distinct parameters that you can pass on the querystring. – Jason May 06 '21 at 23:15

1 Answers1

2

HTTP Get methods should not contain a body as referenced in this thread: HTTP GET with request body.

When creating Get API methods it is better to use [FromQuery]-FromQueryAttribute Class this will bind the data to primitive types. So you will need 3 separate parameters. If you want to bind to an object you will need to create a custom ModelBinder.

This doc from Microsoft goes through your options when trying to bind data in an ASP.Net core/5 API Binding source parameter inference

blockingHD
  • 384
  • 2
  • 11