Skip to lesson

URLs: scheme, host, port, path and query

A support agent pastes a product link into a chat and half of it stops working. A URL looks like one long string, but every piece tells the web where to go. Which piece is which?

Taking a URL apart

A URL (Uniform Resource Locator) is the full address of one thing on the web, written as scheme://host:port/path?query#fragment. Only the scheme and host are always present.

The first example splits one URL into its parts, then shows the request curl builds from it.

Example 1 · A URL, part by part, and what curl sends

Terminal
# https://httpbin.org:443/get?city=Pune&sort=price#results#   scheme   = https#   host     = httpbin.org#   port     = 443#   path     = /get#   query    = city=Pune&sort=price#   fragment = resultscurl -sv "https://httpbin.org:443/get?city=Pune&sort=price#results" -o out.json
Sample output (trimmed)
* Host httpbin.org:443 was resolved.
...
> GET /get?city=Pune&sort=price HTTP/1.1
> Host: httpbin.org
...
< HTTP/1.1 200 OK

What this code does

  • curl used the scheme to choose the protocol, the host and port to find the server (httpbin.org:443), and put the host name in the Host: line.
  • Only the path and query travel in the request line: GET /get?city=Pune&sort=price. The fragment #results is gone; it stays on the client.
  • httpbin.org is a free test server that echoes back what it received, which makes it handy for checking URLs.

Each part answers one question:

  • Scheme: how to talk to the server, usually http or https.
  • Host and port: which computer (a name, looked up with DNS, or an IP address) and which server program on it.
  • Path: which resource on that server, such as /products/214. Paths are case-sensitive on most servers.
  • Query after ? and fragment after #: extra details for the server, and a spot inside the page for the browser.

Trace the code

Split a URL with Java

Step through the program and watch each part appear.

import java.net.URI;
public class UrlParts {
public static void main(String[] args) {
URI url = URI.create("https://shoplite.in:8443/products?category=books#reviews");
System.out.println("scheme = " + url.getScheme());
System.out.println("host = " + url.getHost());
System.out.println("port = " + url.getPort());
System.out.println("path = " + url.getPath());
System.out.println("query = " + url.getQuery());
System.out.println("fragment = " + url.getFragment());
}
}

Variables

url
https://shoplite.in:8443/products?category=books#reviews

Output

(nothing printed yet)

Line 5One string holds the whole address; URI.create splits it for us.

Step 1 of 7
Reading a URL from left to right tells you exactly where the request goes. That is the first check when a request fails: is every part what you meant?

Default ports, and when the port is shown

A port is a number from 0 to 65535 that picks one server program on a computer. The default is 80 for http and 443 for https, and a URL leaves the default out.

The second example shows the default port being left out, then what happens when the scheme does not match the port.

Example 2 · Default ports, and a scheme on the wrong port

Terminal
curl -sv https://example.com:443/ -o page.htmlcurl https://example.com:80/curl http://example.com:443/
Sample output (Windows curl; trimmed)
> GET / HTTP/1.1
> Host: example.com
< HTTP/1.1 200 OK

curl: (35) schannel: next InitializeSecurityContext failed: SEC_E_INVALID_TOKEN (0x80090308) - The token supplied to the function is invalid

<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
...

What this code does

  • Writing :443 with https changes nothing: it is the default, so curl even leaves it out of the Host: line.
  • https on port 80 fails with error 35: curl starts the encrypted handshake, but port 80 answers in plain HTTP. On macOS the message reads wrong version number instead; the cause is the same.
  • http on port 443 reaches the encrypted port with a plain request; this server is kind enough to answer with an explanation. Fix both by matching the scheme to the port, or leaving the port out.

https://example.com/ and https://example.com:443/ are the same address. You only see a port when it is not the default, which is common while developing: http://localhost:8000/ or http://localhost:8080/api/products.

The scheme and the port must match what the server speaks. Sending https to a plain http port, or the other way round, fails, because the two sides do not understand each other's first message.

Query strings: key=value pairs

A query string is the part of a URL after ?, made of key=value pairs joined by &, such as ?city=Pune&sort=price. Servers use it for searches, filters and page numbers.

The next two examples send a repeated key, then show what an unquoted & does in the terminal.

Example 3 · Query parameters, including a repeated key

Terminal
curl "https://httpbin.org/get?tag=maths&tag=physics&page=2"
Sample output (trimmed)
{
  "args": {
    "page": "2",
    "tag": [
      "maths",
      "physics"
    ]
  },
  ...
  "url": "https://httpbin.org/get?tag=maths&tag=physics&page=2"
}

What this code does

  • httpbin split the query string at each & and each =. It found two keys: page with one value and tag with two.
  • A repeated key arrives as a list (["maths", "physics"]). Filters such as "show maths and physics books" are often sent this way.

Example 4 · Trap: an unquoted & in the terminal

Terminal
curl https://httpbin.org/get?city=Pune&sort=price
Sample output (Git Bash; macOS zsh may say "no matches found" instead)
[1] 54771
{
  "args": {
    "city": "Pune"
  },
  ...
  "url": "https://httpbin.org/get?city=Pune"
}

What this code does

  • bash saw & and ran curl https://httpbin.org/get?city=Pune in the background ([1] 54771 is the background job number). sort=price became a separate shell command, so the server never got it.
  • The fix is double quotes around the whole URL: curl "https://httpbin.org/get?city=Pune&sort=price". Quote every URL that has ? or & in it.

A key may appear more than once: ?tag=maths&tag=physics sends two values for tag, and most servers read them as a list. The order of different keys usually does not matter, but the spelling does: City and city are different keys.

In a terminal, always put a URL with a query string inside double quotes. Without them, the shell treats & as "run in the background" and cuts the URL short.

Fix the error

Quote the URL

Read the real error, then pick the line that fixes it.

The second request loses the sort parameter.

curl "https://httpbin.org/get?city=Pune"
curl https://httpbin.org/get?city=Pune&sort=price

Error

[1] 54771
bash: sort=price: command not found

Percent-encoding: spaces, symbols and other languages

Percent-encoding writes a character that has a special meaning in a URL as % plus its byte value in hex: a space is %20 and & is %26.

The next example sends a space, an & and Hindi text inside values, and shows how each one is encoded.

Example 5 · Spaces, & and Hindi text, encoded

Terminal
curl "https://httpbin.org/get?q=fees & dues"curl -G "https://httpbin.org/get" --data-urlencode "q=fees & dues" --data-urlencode "city=बेंगलुरु" -sv -o out.jsoncurl "https://httpbin.org/get?q=fees%20%26%20dues&room=B%2312"
Sample output (trimmed to the important lines)
curl: (3) URL rejected: Malformed input to a URL function

> GET /get?q=fees+%26+dues&city=%E0%A4%AC%E0%A5%87%E0%A4%82%E0%A4%97%E0%A4%B2%E0%A5%81%E0%A4%B0%E0%A5%81 HTTP/1.1

{
  "args": {
    "q": "fees & dues",
    "room": "B#12"
  },
  ...
}

What this code does

  • The first URL has raw spaces, so curl refuses it (error 3) before sending anything. A browser would quietly encode the spaces for you.
  • --data-urlencode encodes each value: the space became +, the & became %26, and each of the 8 Hindi characters became 3 encoded bytes. -G puts the values in the query string.
  • In the last URL the encoding is written by hand. The server decodes %20, %26 and %23 back, so it receives fees & dues and B#12 exactly.

Some characters have a special meaning in a URL: ? starts the query, & separates parameters, = separates key and value, # starts the fragment and / separates path parts. Spaces are not allowed at all. ? is %3F and # is %23. Non-English text such as बेंगलुरु is first turned into UTF-8 bytes and each byte is encoded, so one Hindi letter becomes %E0%A4%AC. Inside query strings, + also means a space.

Browsers encode what you type in the address bar for you and show it back decoded. curl does not; use --data-urlencode (with -G for a query string) to let curl do it.

Absolute and relative URLs, and the fragment

A relative URL leaves parts out and is completed from the URL of the current page, called the base URL. An absolute URL has everything from the scheme on.

The last example sends a URL with a #fragment and shows that the part after # never leaves the client.

Example 6 · The fragment never reaches the server

Terminal
curl -sv "https://example.com/?lang=en#contact" -o page.htmlcurl "https://httpbin.org/get?room=B#12"
Sample output (trimmed)
> GET /?lang=en HTTP/1.1
> Host: example.com

{
  "args": {
    "room": "B"
  },
  ...
}

What this code does

  • curl removed #contact before sending: the request line asks only for the path and query, /?lang=en. A browser does the same, then scrolls to the element named contact after the page arrives.
  • In the second URL, #12 was meant to be part of the room number, but # starts a fragment, so the server received only room=B. Encoding it as %23 (Example 5) fixes it.

Web pages use relative URLs all the time for their own links and pictures. With the base https://shop.example.in/products/list.html?page=1:

  • item.html → https://shop.example.in/products/item.html (same folder).
  • /cart → https://shop.example.in/cart (a leading / starts from the host).
  • ../help/faq.html → https://shop.example.in/help/faq.html (.. goes up one folder).
  • ?page=2 → https://shop.example.in/products/list.html?page=2 (same page, new query).
The fragment (#reviews) is used only by the browser, to jump to a spot inside the page. It is never sent to the server. So a # typed by mistake inside a value cuts off everything after it.

Next step

Check your understanding

12 quick questions