Ian Boggs
Ian Boggs

Reputation: 542

REST API Url Pattern

I'm currently designing a REST API interface and am trying to decide on the best way to structure the calls.

I am providing the ability to create a record in the system. However, the record could be created using two different sets of data, depending on which system calls the data.

I was thinking of providing a url in the format

/api/create/auto
/api/create/manual

and using the same function (the auto/manual would be a route parameter) to process, with the body containing the appropriate data. However I was wondering if providing url in the format

/api/createauto
/api/createmanual

and use two separate functions to process is a better format to use. The processing involved is fairly simple, I'm just going to take the incoming data and pass it to one of two stored procedures in the database to do the work. If I do it the first way I have to add validation to ensure only the correct values are passed.

Upvotes: 2

Views: 1408

Answers (2)

George Tuan
George Tuan

Reputation: 56

REST Api best practices actually states that endpoints should be nouns not verbs. In best practices case the endpoint should be

POST /api/object_name

Then you can either specify auto versus manual through a field in the data body. Otherwise if you really want to specify auto or manual through the URL, you could use a query parameter though I don't think that's actually kosher.

POST /api/object_name?type=auto

Your best bet, would probaby add the type to the json body (assuming you're using json). so something like

{"type":"auto","data":{data json object}}

Upvotes: 4

M B
M B

Reputation: 2326

You can have your URLs set up the first way and still point to 2 different functions by using the Route attributes to set the route for each function.

[Route("create/auto")]
public void AutoCreate(){//do something...}

Link to Microsoft docs on Attribute Routing

Upvotes: 1

Related Questions