URL
ctx.Request.URL provides structured access to the current request URL.
Instead of manually analysing the address received, URL It displays its components by specific methods. This allows you to consult the full URL, the schema, the origin, the host, the port, the route and the query string directly.
func GET(ctx *gox.Context) gox.Page {
origin := ctx.Request.URL.Origin()
path := ctx.Request.URL.Path()
return <p>{{ origin }}{{ path }}</p>
} Structure
ctx.Request.URL sets out the following API:
URL
{
String() string
Scheme() string
Host() string
Origin() string
Hostname() string
Port() string
Path() string
RawPath() string
QueryString() string
} All your methods are read-only and return information corresponding to the URL of the current request.
Methods
| Method | Description |
|---|---|
| String() | Returns the full representation of the URL. |
| Scheme() | Return the request schema. |
| Host() | Returns the host, including the port when present. |
| Origin() | Returns the schema and host as a single unit. |
| Hostname() | Return the host's name without the port. |
| Port() | Returns the explicit URL port. |
| Path() | Returns the processed route of the request. |
| RawPath() | Returns the encoded route representation when available. |
| QueryString() | Returns the query string without the character ?. |
String()
Firma:func (URL) String() string
String() returns the full representation of the request URL.
It includes the available components of the URL, such as the schema, host, route and query string.
func GET(ctx *gox.Context) gox.Page {
// URL: https://example.com:8443/products?page=2
url := ctx.Request.URL.String()
// url == "https://example.com:8443/products?page=2"
return <p>{{ url }}</p>
} Use the other methods when you only need a specific part of the URL.
Scheme()
Firma:func (URL) Scheme() string
Scheme() returns the schema used by the request.
The usual values are http and https. The result does not include the characters ://.
func GET(ctx *gox.Context) gox.Page {
// URL: https://example.com/products
scheme := ctx.Request.URL.Scheme()
// scheme == "https"
return <p>{{ scheme }}</p>
} To get the schema along with the host uses Origin().
Host()
Firma:func (URL) Host() string
Host() return the host of the request.
When the URL contains an explicit port, this is part of the returned value. The schema is not included.
func GET(ctx *gox.Context) gox.Page {
// URL: https://example.com:8443/products
host := ctx.Request.URL.Host()
// host == "example.com:8443"
return <p>{{ host }}</p>
} Unlike Hostname(), Host() may include the port.
To get the schema and host as a single unit uses Origin().
Origin()
Firma:func (URL) Origin() string
Origin() returns the URL origin.
The origin is composed of the schema and the host. When there is an explicit port, it is also part of the result.
func GET(ctx *gox.Context) gox.Page {
// URL: https://example.com:8443/products?page=2
origin := ctx.Request.URL.Origin()
// origin == "https://example.com:8443"
return <p>{{ origin }}</p>
} Origin() does not include the route or the query string.
It can be used as a basis for building an absolute URL within the same source:
func GET(ctx *gox.Context) gox.Page {
loginURL := ctx.Request.URL.Origin() + "/login"
return <a href={{ loginURL }}>Login</a>
} Use Origin() instead of manually rebuilding the origin by Scheme(), Hostname() and Port().
Hostname()
Firma:func (URL) Hostname() string
Hostname() returns only the name of the host.
Unlike Host(), the result never includes the port.
func GET(ctx *gox.Context) gox.Page {
// URL: https://example.com:8443/products
hostname := ctx.Request.URL.Hostname()
// hostname == "example.com"
return <p>{{ hostname }}</p>
} Use Hostname() when you need to compare or process the server name regardless of the port used.
Port()
Firma:func (URL) Port() string
Port() returns the port explicitly specified in the URL.
func GET(ctx *gox.Context) gox.Page {
// URL: https://example.com:8443/products
port := ctx.Request.URL.Port()
// port == "8443"
return <p>{{ port }}</p>
} When the URL does not contain an explicit port, the method returns an empty string.
Port() does not infer the default HTTP or HTTPS ports. For example, an HTTPS request that does not explicitly include :443 does not return 443.
Path()
Firma:func (URL) Path() string
Path() returns the processed route of the request.
It does not include the schema, the host or the query string.
func GET(ctx *gox.Context) gox.Page {
// URL: https://example.com/products/15?page=2
path := ctx.Request.URL.Path()
// path == "/products/15"
return <p>{{ path }}</p>
} Path() represents the URL route, but does not contain information on how GOX resolved that route.
To check the pattern, dynamic parameters and the resolved destination uses ctx.Request.Route.
RawPath()
Firma:func (URL) RawPath() string
RawPath() returns the coded representation of the route when it is available.
It can be used when it is necessary to distinguish between the processed route and the coded form received by the server.
func GET(ctx *gox.Context) gox.Page {
// URL recibida: https://example.com/products/hello%20world
path := ctx.Request.URL.Path()
rawPath := ctx.Request.URL.RawPath()
// path puede ser "/products/hello world"
// rawPath puede ser "/products/hello%20world"
return <main>
<p>Path: {{ path }}</p>
<p>Raw path: {{ rawPath }}</p>
</main>
} Where there is no independent encoded representation, RawPath() can return an empty string.
For most operations it should be used Path().
QueryString()
Firma:func (URL) QueryString() string
QueryString() returns the full URL query string.
The result does not include the initial character ? and does not separate the individual parameters.
func GET(ctx *gox.Context) gox.Page {
// URL: https://example.com/products?page=2&sort=name
queryString := ctx.Request.URL.QueryString()
// queryString == "page=2&sort=name"
return <p>{{ queryString }}</p>
} When the URL does not contain a query string, the method returns an empty string.
To access each parameter individually uses ctx.Request.Query.
Full example
The following example shows how to consult all the components exposed by URL:
func GET(ctx *gox.Context) gox.Page {
// URL: https://example.com:8443/products?page=2&sort=name
url := ctx.Request.URL
return <main>
<p>URL: {{ url.String() }}</p>
<p>Scheme: {{ url.Scheme() }}</p>
<p>Host: {{ url.Host() }}</p>
<p>Origin: {{ url.Origin() }}</p>
<p>Hostname: {{ url.Hostname() }}</p>
<p>Port: {{ url.Port() }}</p>
<p>Path: {{ url.Path() }}</p>
<p>Raw path: {{ url.RawPath() }}</p>
<p>Query: {{ url.QueryString() }}</p>
</main>
} For the URL used in the example, the main values are:
| Method | Outcome |
|---|---|
String() | https://example.com:8443/products?page=2&sort=name |
Scheme() | https |
Host() | example.com:8443 |
Origin() | https://example.com:8443 |
Hostname() | example.com |
Port() | 8443 |
Path() | /products |
QueryString() | page=2&sort=name |
The value of RawPath() It depends on the existence of a coded representation independent of the route.
Best practices
- Use
String()only when you need full URL representation. - Use
Origin()When you need the schema and the host as a single unit. - Use
Host()when the explicit port is also relevant. - Use
Hostname()when you only need the server name. - Use
Path()to work with the processed route. - Use
RawPath()only when you need to inspect the original encoding. - Use
QueryString()when you need the full query string. - Use
ctx.Request.Queryto access individual parameters. - Avoid manually reconstructing values that already exposes
URL.
To learn more about RequestSee your documentation. To learn more about Context, check your documentation.