All Questions
56
questions
64
votes
13
answers
88k
views
Go gin framework CORS
I'm using Go gin framework gin
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Content-Type", "application/json")
c.Writer.Header()....
13
votes
1
answer
15k
views
How to unit test a Go Gin handler function?
I have a controller function like this....
func GetMaterialByFilter(c *gin.Context) {
queryParam := weldprogs.QueryParam{}
c.BindQuery(&queryParam)
materialByFilter, getErr := ...
10
votes
4
answers
20k
views
Go-Gin read request body many times
I am trying to restore the context with it's data after performing validation on it's data.I need the data to keep moving as need it later on in the next function.
I am new to golang and the below ...
49
votes
8
answers
49k
views
How to pass arguments to router handlers in Golang using Gin web framework?
I'm using Gin, https://gin-gonic.github.io/gin/, to build a simple RESTful JSON API with Golang.
The routes are setup with something like this:
func testRouteHandler(c *gin.Context) {
// do smth
...
20
votes
2
answers
11k
views
Gin router: path segment conflicts with existing wildcard
I want to make my app to serve below things.
a.com => serve /www to a browser so that the browser can seek /www/index.html)
a.com/js/mylib.js => serve /www/js/mylib.js to a browser
a.com/api/v1/disk =...
13
votes
1
answer
8k
views
How to set mock gin.Context for BindJSON
I'm setting up testing in Go.
I use go-sqlmock to test mysql connection and Go Gin as framework. Now I try to test mysql insert logic.
The problem is I need to set mock gin.Context which is used for ...
11
votes
1
answer
19k
views
Return custom error message from struct tag validation
I'm using Go 1.17 with Gin and I want to implement a struct validation before sending the data to my database. I took the example from Gin documentation.
In the struct we can declare different tags to ...
22
votes
2
answers
44k
views
How to log response body in gin
I need to log the response body in a middleware of gin, but I don't find how to get the response body. Can anyone help?
I am using a middleware like this:
func Logger() gin.HandlerFunc {
return ...
16
votes
2
answers
32k
views
How to handle errors in Gin middleware
I want to grab all http errors on each route without rewrite each time if 400 then if 404 then if 500 then etc... so I have an ErrorHandler() function inside each route handler:
func (h *Handler) List(...
15
votes
2
answers
34k
views
How to render static files within Gin router?
I want to serve a JSON file with gin server. And set some customize values in the HTML file. Use JavaScript in it to call the JSON file.
My application structure:
.
├── main.go
└── templates
├── ...
13
votes
1
answer
24k
views
How to group routes in gin?
I want to group my routes in different files, so the main file won't be very messy.
I want something like this in their own files:
v1 := router.Group("/v1")
{
v1.Group("users", ...
9
votes
1
answer
22k
views
gin/golang - Empty Req Body
I'm new to Go and Gin, and am having trouble printing out the full request body.
I want to be able to read the request body from third party POST, but I'm getting empty request body
curl -u dumbuser:...
5
votes
3
answers
19k
views
Gin wildcard route conflicts with existing children [duplicate]
I'd like to build a gin program which serves the following routes:
r.GET("/special", ... // Serves a special resource.
r.Any("/*", ... // Serves a default resource.
However, such a program ...
3
votes
3
answers
4k
views
How to assert error type json.UnmarshalTypeError when caught by gin c.BindJSON
I'm trying to catch binding errors with gin gonic and it's working fine for all validation errors from go-playground/validator/v10 but i'm having an issue catching errors when unmarshalling into the ...
1
vote
1
answer
3k
views
GO storing data returns EOF
I'm new to golang and I'm trying to build rest api, So far GET endpoints are working for me fine, but I'm having difficulties with POST method(creating user):
That's how my User struct is looking:
...