These are chat archives for gin-gonic/gin

24th
May 2015
Oleg Lebedev
@olebedev
May 24 2015 05:28
@manucorporat yep, thanks! :smile:
Pavel Pavlenko
@pavlik
May 24 2015 17:07
Hi to everyone! I need help. How to redirect to new post, instead substitution template in the current url? https://github.com/pavlik/blogus_primitivus/blob/master/main.go#L100
Manu MA
@manucorporat
May 24 2015 17:17
@pavlik do you want to redirect to /new instead of rendering the template?
try this, instead of c.HTML()
c.Redirect(307, "/new")
c.Redirect(http.StatusTemporaryRedirect, "/new")
Pavel Pavlenko
@pavlik
May 24 2015 17:22
why that status? Yes, I need to redirect to /view/:id of already created post
Manu MA
@manucorporat
May 24 2015 17:23
then:
// 307 = http.StatusTemporaryRedirect
c.Redirect(307, "/view/"+post.ID)
Pavel Pavlenko
@pavlik
May 24 2015 17:24
thank you!
Manu MA
@manucorporat
May 24 2015 17:24

there are different redirect codes, you need 307

The requested resource resides temporarily under a different URI. Since the redirection MAY be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.

if you use 301: permanent redirect, the brower could cache that and automatically all requests to " / " will go to "/view/{SOME CODE}" without even touching the server
This message was deleted

@pavlik dude! I am reading the HTTP standard, and you may have to use 303: 10.3.4 303 See Other

The response to the request can be found under a different URI and SHOULD be retrieved using a GET method on that resource. This method exists primarily to allow the output of a POST-activated script to redirect the user agent to a selected resource. The new URI is not a substitute reference for the originally requested resource. The 303 response MUST NOT be cached, but the response to the second (redirected) request might be cacheable.

Pavel Pavlenko
@pavlik
May 24 2015 17:29
interesting
Pavel Pavlenko
@pavlik
May 24 2015 18:30
When i make this: c.Redirect(307, "/view/12")
i receive: "page with this message 405 method not allowed"
Whats wrong?
Manu MA
@manucorporat
May 24 2015 18:46
are you doing GET / ?
@pavlik
Pavel Pavlenko
@pavlik
May 24 2015 18:46
yes
Manu MA
@manucorporat
May 24 2015 18:47
because you created route for r.POST("/", articleSave)
not GET
that is not a gin released issue, it is how the HTTP standard is defined
405 = you are making a GET request to a POST one
Manu MA
@manucorporat
May 24 2015 18:49
so, people will do: use POST / ---> save to dB --> redirect to GET /view/ID
right?
Pavel Pavlenko
@pavlik
May 24 2015 18:50
right
Manu MA
@manucorporat
May 24 2015 18:50
so why are you testing GET / ?
that will never work unless you define a router like:
router.GET("/", handler)
that is why you are getting a 405, not because the redirect
use something like this: https://luckymarmot.com
to test your API
Pavel Pavlenko
@pavlik
May 24 2015 18:52
/ - is index page of the site. I need to redirect to this route r.GET("/view/:id", articleGet)
Manu MA
@manucorporat
May 24 2015 18:53
do you have the logs enabled?
can i see them?
Pavel Pavlenko
@pavlik
May 24 2015 18:57
I made corrections in code https://github.com/pavlik/blogus_primitivus/blob/master/main.go#L109
This work for me
[GIN-debug] GET   /new                      --> main.articleNew (3 handlers)
[GIN-debug] GET   /edit/:id                 --> main.articleEdit (3 handlers)
[GIN-debug] GET   /delete/:id               --> main.articleDelete (3 handlers)
[GIN-debug] GET   /view/:id                 --> main.articleGet (3 handlers)
[GIN-debug] GET   /                         --> main.articlesGetAll (3 handlers)
[GIN-debug] POST  /                         --> main.articleSave (3 handlers)
[GIN-debug] Listening and serving HTTP on :8080
[GIN] 2015/05/24 - 21:55:52 | 200 |   4.084299ms | [::1]:55949 |   GET     /
[GIN] 2015/05/24 - 21:55:54 | 200 |    413.777ยตs | [::1]:55949 |   GET     /new
0
New post
ID: 16
[GIN] 2015/05/24 - 21:55:58 | 302 |   2.321645ms | [::1]:55949 |   POST    /
[GIN] 2015/05/24 - 21:55:58 | 200 |    1.19972ms | [::1]:55949 |   GET     /view/16
Manu MA
@manucorporat
May 24 2015 18:59
oh! I understand now! we were using the wrong redirect code: we should use 302 or 303
307 does not change the METHOD, so you was making a POST request to /view/ID
instead of a GET one
302 and 303, changes the method to GET
Pavel Pavlenko
@pavlik
May 24 2015 19:00
Ouch! Now I understand!
Manu MA
@manucorporat
May 24 2015 19:01
I am not sure why, but people recommends to use 303
Pavel Pavlenko
@pavlik
May 24 2015 19:01
Thanks for the explanation
Manu MA
@manucorporat
May 24 2015 19:01
you are welcome!