我有一个非常简单的带有 CORS 中间件的应用程序,它似乎可以正常工作并且预检请求按预期工作。但由于某种原因,服务器根据实际请求以 307 响应。在浏览器中重现,来自 postman works correclty。
func main() {
router := gin.Default()
router.Use(CORSMiddleware())
accountsGroup := router.Group("/accounts")
accountsGroup.POST("/", postAccount)
router.Run("localhost:8080")
}
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Credentials", "true")
c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Header("Access-Control-Allow-Methods", "POST,HEAD,PATCH, OPTIONS, GET, PUT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
func postAccount(c *gin.Context) {
response := gin.H{"id": 1}
c.IndentedJSON(http.StatusCreated, response)
}
飞行前反应
实际要求
侃侃无极
相关分类