Requests
NoteIMPORTANT!
The request file should be placed in directoryinternal/http/request
How to get data via path, request (get, post, put, delete), upload
router.GET("/user/{id}", page.NewGetUserAPI());
Get path parameter in controller
userIdStr := c.PathVal("id")
Example below GET request
curl -X GET http://localhost:7789/api/v1/users?name=john&age=10&percent=0.7&open=true
Get data
// name=john
name := c.QueryStr("name")
// age=10
age, _ := c.QueryInt("age")
// open=true
open, _ := c.QueryBool("open")
// percent=0.7
percent, _ := c.QueryFloat("percent")
Get all query params as a map
params := c.Queries() // map[string]string
For repeated keys or key[] notation:
curl -X GET "http://localhost:7789/api/v1/users?tag=go&tag=web&score=9&score=8"
tags := c.QueryStrArr("tag") // []string{"go", "web"}
scores, _ := c.QueryIntArr("score") // []int{9, 8}
bools, _ := c.QueryBoolArr("flag") // []bool
floats, _ := c.QueryFloatArr("ratio") // []float64
data := core.Data{}
_ = c.ParseQuery(&data)
// data["name"] = "john", data["age"] = "10", ...
// repeated keys are stored as []string
Example below POST request
curl -H "Content-Type: application/x-www-form-urlencoded" \
-d "first_name=John&last_name=Coi&age=39&email=john@mail.com&complete=0.8&blocked=false" -v -X POST \
http://localhost:7789/api/v1/users
Form* methods search both query string and POST body:
first_name := c.FormStr("first_name")
last_name := c.FormStr("last_name")
age, _ := c.FormInt("age")
complete, _ := c.FormFloat("complete")
email := c.FormStr("email")
blocked, _ = c.FormBool("blocked")
Post* methods search only the POST body:
first_name := c.PostStr("first_name")
age, _ := c.PostInt("age")
complete, _ := c.PostFloat("complete")
blocked, _ = c.PostBool("blocked")
Get all POST params as a map:
posts := c.Posts() // map[string][]byte
Example below POST request
## Login
curl -X "POST" "http://localhost:7789/api/v1/auth/signin" \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{
"username": "vinh@mail.com",
"password": "My$assWord"
}'
Get data
var signIn request.SignIn
_ := c.ParseBody(&signIn)
AddParam and DeleteParam let middleware inject or remove values before they reach the handler. The optional paramType argument accepts "form", "query", or "body" — omitting it affects both form and query.
// Add to query string and POST args (default)
c.AddParam("user_id", "42")
// Add only to the JSON body
c.AddParam("role", "admin", core.ParamTypeBody)
// Remove from query string only
c.DeleteParam("token", core.ParamTypeQuery)
// Chaining
c.AddParam("a", "1").AddParam("b", "2")
<form method="POST" action="/upload" enctype="multipart/form-data">
<input name="file1" type="file">
<input name="file2" type="file">
<button type="submit">Submit</button>
</form>
Controller
uploads, _ = c.FormUpload("file1", "file2")
for _, upload := range uploads {
fmt.Printf("File info %v \n", upload)
}
<form method="POST" action="/upload" enctype="multipart/form-data">
<input name="file[]" type="file" multiple>
<button type="submit">Submit</button>
</form>
Controller
uploads, _ = c.FormUpload()
for _, upload := range uploads {
fmt.Printf("File info %v \n", upload)
}