Requests
NoteIMPORTANT!
The request file should be placed in directoryapp/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
sort := c.QueryBool("open")
// percent=0.7
percent := c.QueryFloat("percent")
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
Get data
// first_name=John&last_name=Coi&age=39&email=john@mail.com&complete=0.8&blocked=false
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")
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)
<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)
}