Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.
This repository was archived by the owner on May 21, 2025. It is now read-only.

Setting multiple cookies  #204

@michaelCaleyWhaley

Description

@michaelCaleyWhaley

Trying to set multiple cookies is resulting in only one cookie being returned via api gateway. Is there a specific method I am supposed to use other than c.SetCookie?

package main

import (
	"context"
	"log"
	"net/http"
	"os"
	"utilitor/controllers/code"
	"utilitor/controllers/contacts"
	"utilitor/initialisers"
	"utilitor/middleware"

	"github.com/aws/aws-lambda-go/events"
	"github.com/aws/aws-lambda-go/lambda"
	ginadapter "github.com/awslabs/aws-lambda-go-api-proxy/gin"
	"github.com/gin-gonic/gin"
)

var ginLambda *ginadapter.GinLambda
var isLocal = os.Args[len(os.Args)-1] == "--local"

func routes(r *gin.Engine) {
	r.Use(middleware.CrossOrigin)
	r.POST("/api/code", code.Controller)
	r.GET("/api/contact/list", middleware.VerifyAccessToken, contacts.Controller)

	r.GET("/api/test", func(c *gin.Context) {
		// origin := c.Request.Header.Get("Origin")
		// host := c.Request.Header.Get("Host")
		// reqHost := c.Request.Host

		domain := initialisers.GetConfig().CookieDomain
		c.SetCookie("test1", "test", 86400, "/", domain, true, true)
		c.SetCookie("test2", "test", 86400, "/", domain, true, true)

		c.JSON(http.StatusOK, gin.H{
			"cookies": []string{"cookie1", "cookie2"},
		})
	})
}

func init() {
	initialisers.LoadEnvVars()
	log.Printf("Gin cold start")
	r := gin.Default()
	routes(r)

	if isLocal {
		r.Run("localhost:4000")
		return
	}
	ginLambda = ginadapter.New(r)
}

func Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
	return ginLambda.ProxyWithContext(ctx, req)
}

func main() {
	if isLocal {
		return
	}
	lambda.Start(Handler)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions