Search code examples
c#web-serviceshttp-posthttp-get

Why should i use get instead of post?


I have a web service which is developed in mvc c#, the purpose of the service is to retrieve and insert data to sql. All methods are using http attributes, so my question is, if all methods are using [HttpPost] and is working fine, when do i should use [HttpGet].

    [HttpPost]
    [Route("GetVehiculos")]
    public HttpResponseMessage WSGetVehiculos()
    {
        VehiculosDAO vehiculosDAO = new VehiculosDAO();
        List<Vehiculo> lstVehiculo = new List<Vehiculo>();
        string codError = "0";
        string mensaje = "";
        try
        {
            lstVehiculo = vehiculosDAO.GetVehiculos();
            
            mensaje = "OK";
        }
        catch (Exception ex)
        {
            codError ="1";
            Log.WriteToFileLogGeneral(ex.Message);

        }

        return Request.CreateResponse(HttpStatusCode.OK, new { Msg = mensaje, CodError = 
        codError,Data= lstVehiculo } );
    }

Thanks in advance


Solution

  • GET is used for viewing something, without changing it, while POST is used for changing something. For example, a search page should use GET to get data while a form that changes your password should use POST. Essentially GET is used to retrieve remote data, and POST is used to insert/update remote data.