trx
trx

Reputation: 2157

REST API to auto generate a value using post

I will need to create an API that will pass auto generated values to the Text box on the page. Project name is the field we will be passing the auto generated fields. enter image description here

I am new to Web API but I have created the REST API to query a Database and return values using get method. So here everytime the End point is called it should return the auto generated Project name. How to approach this.

Upvotes: 0

Views: 685

Answers (1)

MaVVamaldo
MaVVamaldo

Reputation: 2535

in REST you'll want to use the POST verb whenever you need to create a resource and let the server choose its name. In the HTTP implementation of REST, the server will return the resource name (which is a URI) in the location Header.

If you want to use this approach, this is the way to follow.

The parameters required for the name generation are significant to understand whether this is the best approach or not (is it the case to create a project resource holding these parameters? Does it make sense?). For example, if you need to create a new project you could use the following design:

POST http://authority/rootpath/projects

{    
   name:"prj name",
   relevance:"high",
   dependencies: [...]       
}

expecting to have back in the response Headers

location: http://authority/rootpath/projects/prj_name123

However, maybe you don't want to create a project at all and you just want ask the server for a new name. In this case I think that GETting is a more coherent approach.

GET http://authority/rootpath/projectNames?a='some'&b='useful'&c='parameters'

this request will be followed by a response holding a resource representation like so

{    
   name:"prj name"
}

Upvotes: 1

Related Questions