Skip to main content
gFly v1.18.1
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage
Edit page

Requests

Note
IMPORTANT!
The request file should be placed in directory internal/http/request

How to get data via path, request (get, post, put, delete), upload

Data Path parameter

router.GET("/user/{id}", page.NewGetUserAPI());

Get path parameter in controller

userIdStr := c.PathVal("id")

Data Query parameter

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

Query array values

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

Parse query into Data map

data := core.Data{}
_ = c.ParseQuery(&data)
// data["name"] = "john", data["age"] = "10", ...
// repeated keys are stored as []string

Data POST parameter

Form-encoded body

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

Post JSON parameter

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)

Mutating request parameters

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

Upload form

Form each single file

<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 multi-file

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