Cross-Origin Resource Sharing (CORS)

go-zero offers three ways to handle CORS:

  • rest.WithCors(origin ...string)
    • Sets the allowed origins for cross-origin requests.
  • rest.WithCorsHeaders(headers ...string)
    • Sets the allowed headers for cross-origin requests.
  • rest.WithCustomCors(middlewareFn func(header http.Header), notAllowedFn func(http.ResponseWriter), origin ...string)
    • Configures complex CORS requirements.

How to Customize CORS Headers?

You can add allowed headers for cross-origin requests using go-zero‘s rest.WithCorsHeaders(headers ...string).

Example code:

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/zeromicro/go-zero/core/conf"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. "github.com/zeromicro/go-zero/rest"
  8. "github.com/zeromicro/go-zero/rest/httpx"
  9. )
  10. func main() {
  11. var c rest.RestConf
  12. conf.MustLoad("config.yaml", &c)
  13. server := rest.MustNewServer(c, rest.WithCorsHeaders("UserHeader1", "UserHeader2"))
  14. defer server.Stop()
  15. server.AddRoutes(
  16. []rest.Route{
  17. {
  18. Method: http.MethodPost,
  19. Path: "/test",
  20. Handler: func(w http.ResponseWriter, r *http.Request) {
  21. logx.Info("Request received")
  22. httpx.OkJsonCtx(r.Context(), w, "1")
  23. },
  24. },
  25. },
  26. )
  27. fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
  28. server.Start()
  29. }

go-zero version: >= v1.7.1

How to Customize CORS Domains?

You can set allowed origins for cross-origin requests using go-zero’s rest.WithCors(origins …string).

Example code:

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/zeromicro/go-zero/core/conf"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. "github.com/zeromicro/go-zero/rest"
  8. "github.com/zeromicro/go-zero/rest/httpx"
  9. )
  10. func main() {
  11. var c rest.RestConf
  12. conf.MustLoad("config.yaml", &c)
  13. // Set allowed origins for cross-origin requests
  14. server := rest.MustNewServer(c, rest.WithCors("example.com"))
  15. defer server.Stop()
  16. server.AddRoutes(
  17. []rest.Route{
  18. {
  19. Method: http.MethodPost,
  20. Path: "/test",
  21. Handler: func(w http.ResponseWriter, r *http.Request) {
  22. logx.Info("Request received")
  23. httpx.OkJsonCtx(r.Context(), w, "1")
  24. },
  25. },
  26. },
  27. )
  28. fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
  29. server.Start()
  30. }

How to Customize Complex CORS Settings?

You can configure complex CORS behavior using go-zero’s rest.WithCustomCors.

Example code:

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/zeromicro/go-zero/core/conf"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. "github.com/zeromicro/go-zero/rest"
  8. "github.com/zeromicro/go-zero/rest/httpx"
  9. )
  10. func main() {
  11. var c rest.RestConf
  12. conf.MustLoad("config.yaml", &c)
  13. server := rest.MustNewServer(c, rest.WithCustomCors(func(header http.Header) {
  14. header.Set("Access-Control-Allow-Origin", "*")
  15. header.Add("Access-Control-Allow-Headers", "UserHeader1, UserHeader2")
  16. header.Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH")
  17. header.Set("Access-Control-Expose-Headers", "Content-Length, Content-Type")
  18. }, nil, "*"))
  19. defer server.Stop()
  20. server.AddRoutes(
  21. []rest.Route{
  22. {
  23. Method: http.MethodPost,
  24. Path: "/test",
  25. Handler: func(w http.ResponseWriter, r *http.Request) {
  26. logx.Info("Request received")
  27. httpx.OkJsonCtx(r.Context(), w, "1")
  28. },
  29. },
  30. },
  31. )
  32. fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
  33. server.Start()
  34. }