Charlene
Assistant Engineer
Assistant Engineer
  • UID626
  • Fans0
  • Follows1
  • Posts53
Reads:890Replies:0

PhalGo-Request

Created#
More Posted time:Sep 29, 2016 11:24 AM
PhalGo-Request encapsulates Echo, in an aim to reduce the amount of code from repeated operations of **getting request parameters, converting types and judging parameter validity**. PhalGo-Request supports three types of request parameters, namely **GET, POST and JSON ** and adopts coherent operations to get the parameters required by APIs while reducing the amount of code.
Request initialization
PhalGo is flexible. You can use PhalGo-Request, or Echo native parameters to get requests (RESTful style only supports Echo native parameters to get requests). If you adopt PhalGo-Request, you need to perform initialization in the interface beforehand:
return func(c echo.Context) error {
    Request := phalgo.Requser{Context:c}
}


Get GET and POST parameters
Speaking of two ways of parameter transmission, we all know that they are GET and POST methods. We can get GET or POST request parameters through the methods below:
//Get GET request parameters, string accepted
id := Request.GetParam("id").GetString()
//Get POST request parameters, string accepted  
id := Request.PostParam("id").GetString()


Of course, if you don’t want to separate GET and POST parameters, you can prioritize GET over POST.
id := Request.Param("id").GetString()

In the final step of getting requests, you need to designate the desired type of the request parameter. PhalGo-Request now supports three types, namely **String, Int and Float**, to help to conveniently process parameter types without additional type conversion operations. The method of usage is as follows:
id := Request.PostParam("id").GetString()
id := Request.PostParam("id").GetInt()
id := Request.PostParam("id").GetFloat()


Processing JSON parameters
Speaking of request processing, we cannot skip JSON request type. In many complicated business scenarios, the interface needs to accept a list which is of course a JSON object. For example, if we want to encrypt the request parameters, we usually put all the request parameters into a JSON for encryption, pass it over and decrypt it for use. So JSON is indispensable in request parameter processing. PhalGo-Request also realizes this and thus encapsulates JSON processing. Let’s take a look at a simple example: how to get the JSON parameter from the request parameter:
//Get params json strings through GET requests
params := Request.GetParam("params").GetString()
//Inject JSON strings  
Request.SetJson(params)
//Get the desired parameter types through JsonParam and GetJsonString
id := Request.JsonParam("user.id").GetJsonString()


To get JSON parameter, we need to use JsonParam function. JsonParam accepts a string and the string is separated by “.”, indicating the hierarchical relationship of JSON. For better illustration, we can refer to the example below:
json := `{
    "userlist": {
        "miaomi": {
            "username": "dog my cats"
        }
    }
}`
Request.SetJson(json)
//In this way, we can get the string "dog my cats"
Request.JsonParam("userlist.miaom.usernamei").GetJsonString()


JSON also supports table splitting of **String, Int and Float** types and you only need to use the corresponding **GetJsonString, GetJsonInt and GetJsonFloat**. In addition, the JSON type also includes the **GetJson** method of the JSON type. GetJson will return a JS instance through which we can get more types such as slices and maps.
For more processing of JS instances, you can see the subsequent PhalGo-Json section or view the source code.
Guest