How to support multiple HTTP Method submissions for the same API under strict routing

First, an API should only perform one function. HTTP Method is meaningful (for example, in RESTful API design). An API supporting multiple HTTP Method types is usually a sign of poor API design, and it’s recommended to review the API design.

Generally, there is no scenario where an API needs to be associated with multiple HTTP Methods. For example, for user APIs, a CRUD API in RESTful implementation should have 4-5 API definitions, each implementing different business logic. Therefore, there might be the following API definitions for a RESTful API:

  1. API Name Method Path
  2. Create User PUT /user
  3. User List GET /user
  4. User Details GET /user/{uid}
  5. Modify User POST /user/{uid}
  6. Delete User DELETE /user/{uid}

If indeed there is a scenario where an API needs to support multiple HTTP Methods, it can be done by using a comma , to separate each HTTP Method in the method attribute of the Meta tag, for example:

  1. type SaveReq struct {
  2. g.Meta `path:"/user/{uid}" method:"put,post" summary:"Save User" tags:"User Management"`
  3. Uid int64 `dc:"User ID"`
  4. Name string `dc:"Username"`
  5. // ...
  6. }

How to make the Data field return only as an array without a named key-value pair using the default provided Response structure

This can be achieved by using type aliasing.

Source Code: gf/example/httpserver/response-with-json-array

Result Example:

Standard Router - FAQ - 图1