Rodri
Rodri

Reputation: 11

API REST when IDs are URLs

I am developing an API REST but I faced the following problem: in some cases, the ID of my resources are URLs.

The API is used for both, a software where the IDs are in UUID format, and a FileSystem where the only way of identifying resources is by using their path.

So, to retrieve a resource, I use a GET statement like that: - http://localhost/files/{id}

When I am retrieving a document stored in the database of the software, {id} is something like "2ab89005-b938-46c8-9d22-3a12a3c40672", but when the document is in a FileSystem, the {id} can be something like "/documents/2018/april/myfile.pdf".

How can I manage this situation? Until now, I am doing a GET request over the URL "http://localhost/files/" (without {id}) and including the required information in the body in JSON format:

{
    "id": {id}
}

Of course, I know this is not a good practice, but I don't know how can I implement REST architecture in this situation. I am using Java + Spring Boot.

Thank you.


SOLUTION IMPLEMENTED: At the end, I have created the following endpoint: http://localhost/files/?id={id}. The reson of doing this is because the application that is calling me doesn't know if it is asking for a resource stored in a FileSystem or in my database. If the application knows it, I think it is better to implement two endpoints: http://localhost/files/?id={id} (for FileSystem) and http://localhost/files/{id} (for database).

Upvotes: 1

Views: 1688

Answers (1)

Konstantin Pozhidaev
Konstantin Pozhidaev

Reputation: 517

It's realy bad idea to take ability for user chose file in FS because of Path Manipulation Vulnerability.

But if you want to do that, you cat set String as a path variable, and decode your request string:

@RestController
@RequestMapping("test")
public class MainController {

    @GetMapping("/{id}")
    public Mono<String> main(@PathVariable("id") String id){
        return Mono.just(id);
    }
}

enter image description here

Upvotes: 2

Related Questions