课程: Practice It: Go REST API Server
今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
Creating handlers: POST
- We covered get handlers. Now I'll talk about post handlers. A post request is commonly used to create a new resource and in our case we'll use it to create a new product. In order to use a post request to create a new product we need a way to pass in the product details like product code, name, et cetera. But unlike gets, you can't pass it in the URL you have to use a request body. Before we break that down, we first need to add a new route to the initialized routes function in the backend.go file. So in the backend.go file initialize routes and we're going to add the new route. And it's going to look exactly like this one, the first one. So we'll have router handle function. So it's products. And this time we're going to call newProduct. And this one will be for methods POST. It looks just like the route to get all the products but that one uses the get method and this one will use the post method and the router…