0

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));
}
Czeshirecat
  • 497
  • 1
  • 3
  • 16
  • `POST` is a *catch all* verb. – cassiomolin May 12 '16 at 08:02
  • What do you mean by catch all? – Borjante May 12 '16 at 08:08
  • @Czeshirecat Just for clarification: are you sending a JSON from the client to server. Then the server will modify the JSON and return it to the client. Is that what you mean? – cassiomolin May 12 '16 at 08:11
  • @Borjante I mean the `POST` HTTP verb can be used for any arbitrary processing where the other HTTP verbs do not fit well. – cassiomolin May 12 '16 at 08:13
  • @Borjante Have a look at [this answer](http://stackoverflow.com/a/35864478/1426227) for more details. – cassiomolin May 12 '16 at 08:14
  • Thanks everybody for your responses, all were valid. There may be a lot of data uploaded from the client in some cases which can't be done as url eg arrays of json objects. I'll go with POST for those, and GET for simple requests like the example I posted. – Czeshirecat May 14 '16 at 08:13

4 Answers4

2

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).

See the HTTP method definitions in RFC 7231 for more in-depth information.

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

Community
  • 1
  • 1
Jørn Wildt
  • 4,274
  • 1
  • 21
  • 31
1

GET

It seems that your just want to retrieve data in a meaningful representation (response after processing) from the raw data (request from the caller). There is no modification / insertion of data, so GET should be use.

Mango Wong
  • 629
  • 6
  • 16
1

The POST verb seems to be what you want.

If I understand correctly, you want to send a JSON from the client to server. Then the server will modify the JSON and return it to the client.

In REST APIs, POST is commonly used to create a new resource. But it's also a catch-all verb for operations that should not be executed using the other methods.

For more details on using POST to invoke arbitrary processing, have a look at this answer.

Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
1

I would suggest you to use 'HTTPPOST' if you require to process your JSON object else useGETmethod.

Consider this example for using HttpPost method since I process the JSON object to get some info from database.

    [HttpPost]
    public IHttpActionResult Masters([FromBody]Download_Config_UserInfo Info)
    {
        List<TestMaster> testMaster = null;
        ResponseValidation objValidation = new ResponseValidation();
        try
        {
            #region Validation
            objValidation = base.ValidateRequest(Info);
            if (!objValidation.isValid)
                return base.JsonErrorResult(this.MasterName, objValidation.ErrorCode, objValidation.ErrorDesc);
            #endregion

            #region Initialization
            this.Initialization();
            #endregion

            #region Functionality

            //testMaster = this.GetTestMaster();
            testMaster = this.GetTestDateMaster();                
            if (testMaster == null)
                return base.JsonErrorResult(this.MasterName, "E19", "Data Not Available");

            var result = (from a in testMaster
                          select new object[]
                          {
                              a.TestId,
                              a.TestName
                          });
            #endregion

            return base.JsonResult(this.MasterName, this.Fields, result);

        }
        catch (Exception ex)
        {
            loggerService.Error(Info.ToJSON(), this.MasterName, ex);
            return base.JsonErrorResult(this.MasterName, "E20", "Internal Server Error", ex.Message + "_" + ex.StackTrace);
        }
        finally
        {
            testMaster = null; objValidation = null; base.UserMaster = null; base.UserPositionMapping = null;
        }
    }

    #endregion

    #region Functionality

    [NonAction]
    public List<TestMaster> GetTestMaster()
    {
        List<ADM_Retailer> testMaster = null;
        try
        {
            testMaster = this.GetTest();
            if (testMaster == null || (testMaster.Count == 0)) { return null; }

            string weekNo = base.GetWeekNumber();

            return (from a in testMaster
                    select new TestMaster
                    {
                        TestId = a.ARTR_Id,
                        TestName = a.ARTR_Name,
                    }).ToList();
        }
        finally { }
    }
Bharath theorare
  • 524
  • 7
  • 27