12/27/2023 0 Comments Go http decodeAt the end I would like to mention that this is project is in a very early stage and the API may suffer lot of modifications in the future. Both JSON request body decoding and JSON response body encoding will be done by gap package.įor further documentation how to use this package, please check this Our handler function will be triggered on /hello path, will receive no body ( struct) and will send gap.Response with of type hello (concrete private type in this case, but can be exported as well). And the render function of gap would take care to send the JSON response to the client.īut, let’s jump into coding, then it will be more clear what is this about. So, your custom handler would become concrete request type as input and you may return concrete response type as output. Besides the data, this custom response contains also HTTP status code and errors that may have ocurred. Generic type I here is the type of the JSON request body and O is the type of the data returned within the gap.Response. Here is the signature: func(*gap.Request) *gap.Response. gap’s handler functions accept custom request with concrete type and also custom response with concrete data type. So, it can be used with Go’s standard library HTTP implementation. Today, small package which provides a wrapper over custom gap’s HTTP handler functions providing idiomatic HTTP functions at the end. It’s so boring to handle JSON encoding/decoding and possible errors. If you write lot of API’s in Go, you probably realize this without my explanation. When I say hide it’s not about magic, but about having a framework which does this for you so you can focus to the business login. It’s not the point to hide these steps, but to focus to business logic and not to irrelevant tasks like this. So, lot’s of encoding and decoding which can’t be hidden in an easy way, at least not without generics. Then at the end of the request you have do encode your data to JSON response body again. Even worse is that in your RESTful API you don’t get concrete decoded JSON request body as concrete type, but instead you have to parse it every time. In order to get path or query parameters, you have to play around with req, but let’s forget now about that. Res is just an interface providing several methods to set response and req is a concrete implementation of Go’s HTTP request. This problem was possible to solve in some way but never in such elegant way like using generics.įirst, let’s take a look how Go’s idiomatic HTTP handler functions look like: func(res http.ResponseWriter, req *http.Request) For example, implementing a RESTful API using idiomatic Go requires lot of code repetition in order to JSON decode request bodies and JSON encode response bodies. If you are not familiar with Go, you probably wonder why, but if you are familiar, I believe you know. I was embarrassed with standard Go’s HTTP handler functions for quite a while. Few days ago Go 1.18beta1 was released and with it the first official generics support.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |