Oleh  Sobchuk
Oleh Sobchuk

Reputation: 3722

Redirect with data

After successful create user Model (for example) I need to redirect request to... for example root page. But I wanna send message for ex. "User has been created!".

i can redirect with:

c.Redirect(http.StatusCreated, "/")

but how I can add message?

I tried (guess it was bad idea)

c.Set("message": "Message")

and in root page

s.MustGet("message")

but if root page loads without payload message it complain with panic.

Pls suggest best way for redirection with data.

EDIT

Unfortunately c.Set() doesn't work, guess it's because of redirect.

Maybe some one suggest any tip to send data to redirect?

Upvotes: 0

Views: 2021

Answers (2)

Oleh  Sobchuk
Oleh Sobchuk

Reputation: 3722

For case with Redirect it is impossible send data in requests. So in this case uses Session.

According to DOCUMENTATION

r := gin.Default()
store := cookie.NewStore([]byte("secret"))
r.Use(sessions.Sessions("mysession", store))
session.Set("message", "Oh-ho!")
session.Save()

Upvotes: 0

rajeshnair
rajeshnair

Reputation: 1673

You can always call c.GetString("message") instead of c.MustGet("message")

MustGet panics if the key does not exist as opposed to Get which lets you handle existence of key and Get sounds more appropriate for your use-case

Upvotes: 1

Related Questions