Skip to content

En/http redirects #1640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: development
Choose a base branch
from
Open

En/http redirects #1640

wants to merge 21 commits into from

Conversation

Umang01-hash
Copy link
Member

@Umang01-hash Umang01-hash commented Apr 10, 2025

Pull Request Template

Description:

Example:

package main

import (
	"gofr.dev/pkg/gofr"

	"gofr.dev/pkg/gofr/http/response"
)

func main() {
	app := gofr.New()

	app.GET("/old-page", func(ctx *gofr.Context) (any, error) {
		// Redirect to a new URL 
		return response.Redirect{URL: "https://example.com/new-page"}, nil
	})

	app.Run()
}
Screenshot 2025-04-10 at 1 15 45 PM

Additional Information:

  • Mention any relevant dependencies or external libraries used.
  • Include screenshots or code snippets (if necessary) to clarify the changes.

Checklist:

  • I have formatted my code using goimport and golangci-lint.
  • All new code is covered by unit tests.
  • This PR does not decrease the overall code coverage.
  • I have reviewed the code comments and documentation for clarity.

Thank you for your contribution!

@coolwednesday
Copy link
Contributor

coolwednesday commented Apr 10, 2025

@Umang01-hash , Can we also check and add tests for url with path and query params as well as request Bodies, headers etc.....get handled successfully through the redirect. I feel that is not the case right now.

Comment on lines 136 to 140
app.GET("/old-page", func(ctx *gofr.Context) (any, error) {
// Redirect to a new URL with 301 Moved Permanently status
return ctx.Redirect("/new-page", http.StatusMovedPermanently)
})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not have a redirect method. We can just expose response of redirect Type ..... Also, we must ensure the traces are in hierarchy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Till now, all responses in Gofr are a type which is returned. ctx.Redirect is coming from other framework's behaviour where things like ctx.JSON etc are method. Also, we do not ask people to set manual status codes - but, we automatically decide. We should do the same and use a proper status code. The usecase in APIs for redirects are never going to be 301 - I think.

Copy link
Contributor

@coolwednesday coolwednesday Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In redirects, on the client-side :

  • For 301, 302, 303 → body is usually dropped; method becomes GET
  • For 307, 308 → body is preserved; method is unchanged

I think instead of setting internally, we should allow mentioning it in the response itself so that the client browser can process the urls accordingly.

@vikash , @aryanmehrotra — would love to hear your thoughts on this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, we will not find usecase for 307,308. Most of the usecase in API context can actually be of 303 like payment flow etc.

@coolwednesday
Copy link
Contributor

@Umang01-hash , Can we also check and add tests for url with path and query params as well as request Bodies, headers etc.....get handled successfully through the redirect. I feel that is not the case right now.

@Umang01-hash, Can we add test for these cases ? I feel this implementation can fall short in fitting these usecases.


// Set up redirect with specific URL and status code
redirectURL := "/new-location"
statusCode := http.StatusFound // 301
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
statusCode := http.StatusFound // 301
statusCode := http.StatusFound // 302


func TestResponder_RedirectResponse(t *testing.T) {
recorder := httptest.NewRecorder()
r := NewResponder(recorder, http.MethodGet)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also test at least POST and HEAD

Copy link
Contributor

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to Perform Redirects in Gofr?
5 participants