Skip to content
Discussion options

You must be logged in to vote

Hey @dev00, yes this is expected. ctx.URL() returns the request URL as the server sees it, and for server-side requests Go only fills in Path/RawQuery (see the net/http.Request docs: scheme and host live in Host and the connection state, not the URI). No middleware needed for either piece, both are already on huma.Context:

func absURL(ctx huma.Context) url.URL {
	u := ctx.URL()
	u.Host = ctx.Host()
	u.Scheme = "http"
	if ctx.TLS() != nil {
		u.Scheme = "https"
	}
	// behind a TLS-terminating proxy
	if p := ctx.Header("X-Forwarded-Proto"); p != "" {
		u.Scheme = p
	}
	if h := ctx.Header("X-Forwarded-Host"); h != "" {
		u.Host = h
	}
	return u
}

To use it in the handler, stash the huma.Context

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@dev00
Comment options

Answer selected by robtme
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants