This time we will create a simple HTML form and submit it as a POST request.
Create a POST request endpoint
Checkout this repo and create the api/post_full_name.go to handle the POST request. This new file is almost the same as api/get_full_name.go except the JSON is prettified before returning to the client.
Start the Echo server and browse the about page. Fill in the first and last name and then click the submit button. It should show the response JSON as follow.
The project structure
Here is our new project structure with changes highlighted. (The project is renamed to handling-http-request-in-go-echo-example-2, make sure you set the import statement in the Golang source files correctly if you want to change the project name.)
handling-http-request-in-go-echo-example-2/
├── api/ # folder of api endpoints
│ ├── get_full_name.go # api for get full name │ ├── post_full_name.go # api for post full name ├── handler/ # folder of request handlers
│ ├── home_handler.go # handler for home page
│ └── about_handler.go # handler for about page
├── model/ # folder of custom struct types
│ ├── example_request.go # struct type of get_full_name request
│ └── example_response.go # hstruct type of get_full_name response
├── vendor/ # dependencies managed by dep
│ ├── github.com/*
│ └── golang.org/*
├── view/ # folder of html templates │ ├── base.html # base layout template │ ├── home.html # home page template │ └── about.html # about page template ├── Gopkg.lock # dep config file
├── Gopkg.toml # dep config file
└── main.go # programme entrypoint
Setup a POST request endpoint in the Echo framework.
Prettify the JSON byte array.
Add an HTML form and bind it to the new POST request using AJAX in jQuery/javascript.
The complete example could be found on gitlab.com.
About
Boatswain is a SAAS platform where you could VIEW your Docker application Status, ANALYSE the Logs from all the Containers and GET real time Notification on Events.