Requests

Request #

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

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

Path parameter #

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

Get path parameter in controller

userIdStr := c.PathVal("id")

Query parameter #

Example below GET request

curl -X GET http://localhost:7789/api/v1/users?name=john&age=10&sort=-first_name

Get data

// name=john
name := c.QueryStr("name")

// age=10
age, _ := c.QueryInt("age")

// sort=-first_name
sort := c.QueryStr("sort")

POST parameter #

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" -v -X POST \
  http://localhost:7789/api/v1/users

Get data

// first_name=John&last_name=Coi&age=39&email=john@mail.com
first_name := string(c.FormVal("first_name"))
last_name := string(c.FormVal("last_name"))
age, _ := c.FormInt("age")
email := string(c.FormVal("email"))

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)

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