Skip to main content

All Questions

Tagged with
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()....
qwertmax's user avatar
  • 3,400
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 := ...
Hsn's user avatar
  • 1,228
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 ...
bihire boris's user avatar
  • 1,630
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 ...
Niklas9's user avatar
  • 9,226
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 =...
Xeph's user avatar
  • 484
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 ...
jpskgc.v5's user avatar
  • 269
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 ...
John's user avatar
  • 4,861
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 ...
John Zeng's user avatar
  • 1,184
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(...
John's user avatar
  • 4,861
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 ├── ...
02040402's user avatar
  • 789
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", ...
akko's user avatar
  • 639
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:...
Leon Hu's user avatar
  • 137
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 ...
maerics's user avatar
  • 155k
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 ...
michogph's user avatar
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: ...
Ženia Bogdasic's user avatar

15 30 50 per page