Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
When the gin engine's tree is not aware of a path /foo/ , instead of returning a 404, it responds plainly with a 307 Location: /foo (or 301 in case of GET) i.e. telling the client to redirect to /foo
There's no way to tell Gin what to do with such requests through the means any middlewares, like attaching CORS middlewares (a pretty commonly encountered case).
Some solutions I can think of:
Have a way to define what happens on redirect like we currently do with engine.NoRoute(..,) to explicitly define what happens at NotFound.
server := gin.New()
server.OnRedirect(cors.Default())
Apply the middlewares registered at the routerGroup "/" to all the incoming requests, including redirect request.
server := gin.New()
server.Use(cors.Default()) // registered at the "/" routergroup, therefore, redirect requests go through it as well
Open a new tab in the browser and open dev tools there.
Go to the "Console", and run the following code
let response = await fetch('http://localhost:9000/foo/', {
method: 'GET'
})
Expectation
response.text() should be "Hello World!"
Actual result
Access to fetch at 'http://localhost:9000/foo/' from origin 'chrome-extension://pejkokffkapolfffcgbmdmhdelanoaih' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Description
When the gin engine's tree is not aware of a path
/foo/
, instead of returning a 404, it responds plainly with a307 Location: /foo
(or 301 in case of GET) i.e. telling the client to redirect to/foo
There's no way to tell Gin what to do with such requests through the means any middlewares, like attaching CORS middlewares (a pretty commonly encountered case).
Some solutions I can think of:
engine.NoRoute(..,)
to explicitly define what happens at NotFound."/"
to all the incoming requests, including redirect request.How to reproduce
Expectations
Open a new tab in the browser and open dev tools there. Go to the "Console", and run the following code
Expectation
Actual result
Environment