Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post parameter in path or in body

I am creating Rest API but I am confused in URL structure. I have to send just one parameter to server in my Post request. should I send it through path variable or in request body? what are the best practices?

Example Current URL:

api/v1/users/{id}/name/{name}

name is the variable I want to send to server for to change state

Thanks

like image 759
muneebShabbir Avatar asked Feb 22 '17 11:02

muneebShabbir


People also ask

Can a POST request Have path parameters?

Sending parametersYou can send path and query parameters with your requests using the URL field and the Params tab.

Should POST request have body?

HTTP POST request We use POST to create a new resource. A POST request requires a body in which you define the data of the entity to be created. A successful POST request would be a 200 response code. In our weather app, we could use a POST method to add weather data about a new city.

Can POST method have path variable?

@PathVariable can be used in any type of request method (GET, POST, DELETE, etc).


1 Answers

URL usually identifies resource you want to update.

So the data should go inside in request body

To update user name you may send this to server:

POST api/v1/users/{id} HTTP/1.1
Content-Type: application/x-www-form-urlencoded

name=string
like image 126
Andrii Muzalevskyi Avatar answered Sep 22 '22 16:09

Andrii Muzalevskyi