Search code examples
c#restgetasp.net-web-api2put

API method which does not Post anything and does not Get anything


I have next API methods:

  • Restore database (base on file name).
  • Copy something from folder A to folder B

So this are API method which does not Post anything and does not return(Get) anything.

Should I use Post,Put or Get in case like this? Currently I am using Get, is this OK? It make sense to use Get when we return some data, but I don't need to return any data. At the same time I don't need to post any object, I only need identifier.

Restore from folder on server base on 'id' and 'file name':

[HttpGet]//**put?
[Route("api/[controller]/[action]/{restoreFileName}")]
public async Task<IActionResult> RestoreDB(string restoreFileName)
{
    //Restore database from folder A on server base on restoreFileName
    return new OkResult();
}

Copy from folder A to folder B:

[HttpGet]//**put?
[Route("api/[controller]/{id}/[action]/{filename}")]
public async Task<IActionResult> CopyFromAtoB(int id, string fileName)
{
       //Copy from folder A to folder B base on 'id' and 'file name'.
       return new OkResult();
}

Solution

  • The problem is your API doesn't follow REST principles. Endpoints should represent resources on which you can perform different actions (GET, POST, PUT, PATCH, DELETE). Your endpoint:

    [Route("api/[controller]/[action]/{restoreFileName}")]
    

    Doesn't represent any resource. It's just an URI path. Neither controller, action or restoreFileName is a resource.

    So, you have two options.

    1. Just ignore any REST conventions and just create your own custom paths. If you choose this option, you don't need to do anything. You can use any verb you wish, any naming you wish. However, even in such case POST verb is the most natural one (it does make some change to the system).
    2. Refactor your API to follow REST.

    If you choose the second option, then your routes should like like this:

    [HttpPost]    
    [Route("api/fileManager/files/{filename}")]
    

    And use a command which you will send in the POST body to distinguish between operations on the file, so e.g.: {action: "restore"}