Questions tagged [go-gin]

Gin is a HTTP web framework written in Go.

It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.

Docs

GitHub

755 questions
4
votes
2 answers

Changing default header for JSON data in Gin

I've noticed that using Gin to return a response like this: c.JSON(http.StatusOK, jsonData) automatically creates the following header: application/json; charset=utf-8 Is it possible to modify the header somehow to just…
tommyd456
  • 10,443
  • 26
  • 89
  • 163
3
votes
3 answers

CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin'

I am using Golang with gin-gonic/gin web framework in my backend and with React axios in my frontend. I tried to solve it for two days already, but I still get the same error below: CORS policy: Response to preflight request doesn't pass access…
Vertislav
  • 33
  • 5
3
votes
1 answer

Adding traceId from header to logs sent to Loki server in Golang

I have a function called LokiLogs which sends logs to the Loki server. I want to add a traceId to the logs that gets passed as a header X-Trace-ID in every API call. Here's the code I have so far: type StdoutLogger struct { io.Writer } func (l…
Hari Prasanth
  • 88
  • 1
  • 9
3
votes
3 answers

Golang gin receive json data and image

I have this code for request handler: func (h *Handlers) UpdateProfile() gin.HandlerFunc { type request struct { Username string `json:"username" binding:"required,min=4,max=20"` Description string `json:"description"…
Whilez
  • 75
  • 1
  • 6
3
votes
1 answer

failed to access subdomain from main domain: No 'Access-Control-Allow-Origin'

versions go 1.17 github.com/gin-contrib/cors v1.3.1 github.com/gin-gonic/gin v1.7.7 problem I run gin REST API server in my subdomain. React app placed in main domain access to the API server with GET method and POST method, but get cors policy…
Asuha
  • 231
  • 1
  • 5
  • 16
3
votes
2 answers

How to unit test gin handlers for correct validation of uri params?

I need to test a set of gin handlers with dependencies. I have some successful unit tests up and running, mocking the gin context. I notice, however, that ShouldBindURI never works. The key passed to my repo mock is always empty. I find this…
Laura
  • 288
  • 1
  • 4
  • 14
3
votes
1 answer

gin gonic nested templates

Go base lib's template has method Execute which allows to reference another template in order to use template nesting, like follows: template.Template.Execute("base", data) Also see this SO question. How can I achieve the same but inside gin…
Piotr Zakrzewski
  • 3,591
  • 6
  • 26
  • 28
3
votes
0 answers

Request not accepting Authorization header for every second request

Description I have an API which I have created and I have some endpoints protected. The problem I am facing now on the client making a request is that the first request comes through with the Authorization header provided but a second request is…
King
  • 1,885
  • 3
  • 27
  • 84
3
votes
1 answer

how to assign different gin log writer in run time in golang

I'm using gin-gonic in my golang project. I want to record logs in separate files for each day. So I made a go ticker to generate a log.txt file every 24 hours. I used channels to access the generated log file. So I use my file generator function in…
Mohammad Derakhshan
  • 1,262
  • 11
  • 33
3
votes
1 answer

Can't sort in GORM

I have a News structure, I want to display them in descending date order. But he displays them to me by id in the usual way Struct: type News struct { Id int `json:"id" gorm:"primary_key, AUTO_INCREMENT"` ... CreatedAt…
Casper
  • 173
  • 2
  • 11
3
votes
1 answer

How can I pass a context.Context via Gin's context?

I'm trying to figure out the proper way to propagate a context.Context for the purposes of tracing with OpenTelemetry when using Gin. I currently have a gin handler that calls a function and passes a *gin.Context, like so: func (m Handler)…
Andrew DiNunzio
  • 143
  • 4
  • 17
3
votes
1 answer

golang jwt.MapClaims get user ID

After setup a simple has many association where user has_many posts to create a post with user ID seems that is necessary parse the jwt Claims to get the userID and place it on Post creation. So, how to get the user ID from jwt Claims i tried parse…
jjplack
  • 113
  • 2
  • 11
3
votes
1 answer

How to deploy a go web application

I have some doubts, I am developing a go web program on ubuntu, but my server is centos7 When the development is completed, I need to publish it to my centos server. Can I package it directly on ubuntu, and then upload the executable file to the…
januw a
  • 2,056
  • 5
  • 18
  • 39
3
votes
1 answer

how to use the unique tag with a slice of struct in gin Go framework?

I'm trying to use gin framework to validate a slice of struct in order to ensure the slice is unique. With The following code: type CreateOrderParam struct { Items []*CreateItemParam `form:"items"…
Roach
  • 31
  • 4
3
votes
2 answers

How do I distribute tracing between 2 Go microservices using Opentelemetry & Otelgin?

I am trying to distribute tracing between 2 Go microservices using Opentelemetry and Gin-Gonic. Please help, I have come across Otelhttp examples, but I couldn't find examples with Otelgin. Using "go.opentelemetry.io/otel/sdk/trace" as…