GETPOSTPUT Which should be used for receiving a json object to my rest api and send a json object in response

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I m writing my first web API using MVC.

I m aware that POST and PUT are usually implemented to define the HTTP methods for inserting or updating a database behind an API. But all I want to do is receive a JSON object from the caller, do some processing then return another JSON object in response. No database is involved.

I ve tested using both POST and GET in my API controller method and they both work ok but which http method should I actually be using for best practice?

eg

public IHttpActionResult Get(ApiEquipment equipment)
{
    // Convert equipment to a compatible buffer
    return Ok(new ConvertToBuffer(equipment));
}

Answers

GET is useful for SAFE(*) operations where you do not need to pass many parameters to the server - all of the parameters must be in the URL as GET operations do not pass data in the HTTP body.

POST is useful for UNSAFE(*) operations or operations where you need to pass large amounts of data to the server as the data can be passed in the HTTP body.

Use GET for simple queries and calculations with few parameters. Use POST for large payloads or operations that change server state (such as updating something or performing complex bussiness operations).

https://tools.ietf.org/html/rfc7231#section-4 https://tools.ietf.org/html/rfc7231#section-4

(*) SAFE operations are operations that do not change (visible) server state. UNSAFE operations do change (visible) server state.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/37180704/get-post-put-which-should-be-used-for-receiving-a-json-object-to-my-rest-api-and

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils