<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 請求 ### 數據綁定 可以使用 `Context#Bind(i interface{})` 將請求內容體綁定至 go 的結構體。默認綁定器支持基于 Content-Type 標頭包含 application/json,application/xml 和 application/x-www-form-urlencoded 的數據。 下面是綁定請求數據到 `User` 結構體的例子 ```go // User User struct { Name string `json:"name" form:"name" query:"name"` Email string `json:"email" form:"email" query:"email"` } ``` ```go // Handler func(c echo.Context) (err error) { u := new(User) if err = c.Bind(u); err != nil { return } return c.JSON(http.StatusOK, u) } ``` #### JSON 數據 ```go curl \ -X POST \ http://localhost:1323/users \ -H 'Content-Type: application/json' \ -d '{"name":"Joe","email":"joe@labstack"}' ``` #### Form 表單數據 ```go curl \ -X POST \ http://localhost:1323/users \ -d 'name=Joe' \ -d 'email=joe@labstack.com' ``` #### 查詢參數 (Query Parameters) ```go curl \ -X GET \ http://localhost:1323/users\?name\=Joe\&email\=joe@labstack.com ``` ### 自定義綁定器 可以通過 `Echo#Binder` 注冊自定義綁定器。 *示例* ```go type CustomBinder struct {} func (cb *CustomBinder) Bind(i interface{}, c echo.Context) (err error) { // 你也許會用到默認的綁定器 db := new(echo.DefaultBinder) if err = db.Bind(i, c); err != echo.ErrUnsupportedMediaType { return } // 做你自己的實現 return } ``` ### 檢索數據 #### Form 表單數據 表單數據可以通過名稱檢索,使用 `Context#FormValue(name string)` 方法。 *示例* ```go // Handler func(c echo.Context) error { name := c.FormValue("name") return c.String(http.StatusOK, name) } ``` ```go curl \ -X POST \ http://localhost:1323 \ -d 'name=Joe' ``` 當然,你也可以通過實現 `Echo#BindUnmarshaler` 接口來綁定自定義數據類型。 ```go type Timestamp time.Time func (t *Timestamp) UnmarshalParam(src string) error { ts, err := time.Parse(time.RFC3339, src) *t = Timestamp(ts) return err } ``` #### 查詢參數 (Query Parameters) 查詢參數可以通過名稱獲取,使用 `Context#QueryParam(name string)` 方法。 *示例* ```go // Handler func(c echo.Context) error { name := c.QueryParam("name") return c.String(http.StatusOK, name) }) ``` ```go curl \ -X GET \ http://localhost:1323\?name\=Joe ``` 和表單數據一樣,自定義數據類型依然通過 `Context#QueryParam(name string)` 進行綁定。 #### 路徑參數 (Path Parameters) 路徑參數可以通過 `Context#Param(name string) string` 進行檢索。 *示例* ```go e.GET("/users/:name", func(c echo.Context) error { name := c.Param("name") return c.String(http.StatusOK, name) }) ``` ```go $ curl http://localhost:1323/users/Joe ``` ### 數據驗證 Echo 沒有內置的數據驗證功能,但是可以通過 `Echo#Validator` 和[第三方庫](https://github.com/avelino/awesome-go#validation)來注冊一個數據驗證器。 下面例子使用 ?[https://github.com/go-playground/validator](https://github.com/go-playground/validator)? 所展示的框架來做驗證: ```go type ( User struct { Name string `json:"name" validate:"required"` Email string `json:"email" validate:"required,email"` } CustomValidator struct { validator *validator.Validate } ) func (cv *CustomValidator) Validate(i interface{}) error { return cv.validator.Struct(i) } func main() { e := echo.New() e.Validator = &CustomValidator{validator: validator.New()} e.POST("/users", func(c echo.Context) (err error) { u := new(User) if err = c.Bind(u); err != nil { return } if err = c.Validate(u); err != nil { return } return c.JSON(http.StatusOK, u) }) e.Logger.Fatal(e.Start(":1323")) } ``` ```go curl \ -X POST \ http://localhost:1323/users \ -H 'Content-Type: application/json' \ -d '{"name":"Joe","email":"joe@invalid-domain"}' {"message":"Key: 'User.Email' Error:Field validation for 'Email' failed on the 'email' tag"} ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看