KAHIBARO
Discord Login Register

URL Parameters

Understanding URL Parameters

When you build a backend, you often need to receive information from the client as part of the URL itself. URL parameters are one of the main ways to do this.

In this chapter you will learn what URL parameters are, how they differ from other kinds of request data, and how they are used in typical backend routes.


What URL Parameters Are

A URL is usually written like this:

text
https://example.com/users/42

Here, /users/42 is the path. The part 42 is not a fixed word like users. It is a value that can change, for example:

That variable part of the path is what we call a URL parameter or path parameter.

In a route definition, you usually describe it as a pattern, for example (in a Python style):

python
GET /users/{user_id}

The {user_id} is a placeholder that will match values like 1, 42, or 999.

URL parameters belong to the path segment of the URL. They are part of the structure of the endpoint and they usually identify which resource you are working with.

Important rule:
URL (path) parameters are part of the path itself and are used to identify specific resources, such as /users/{id} or /posts/{slug}.


URL Parameters vs Query Parameters vs Request Body

Even though this chapter focuses on URL parameters, you will see different ways to send data to a backend. It is important to understand how URL parameters differ from the others.

URL Parameters

Query Parameters

Request Body

You will see query parameters and request bodies in later chapters. Here, only focus on URL parameters.


AspectURL ParametersQuery ParametersRequest Body
LocationPath, e.g. /users/42After ?, e.g. ?page=2Inside HTTP body
Typical usageIdentify a resourceFilter, sort, search, optionsCreate or update data
Required / optionalUsually requiredOften optionalDepends on endpoint
Visible in URLYesYesNo

Common Use Cases for URL Parameters

URL parameters are usually used when you need to identify a specific item or a combination of hierarchical items.

Identifying a Single Resource

A very common pattern in REST APIs is:

Examples:

text
GET /users/42
GET /products/999
DELETE /orders/123

In each one, the last part of the URL is a parameter:

The server uses this value to look up the corresponding record in the database.

Nested Resources

Sometimes a resource belongs to another resource. For example, comments on a blog post:

text
GET /posts/13/comments
GET /posts/13/comments/2

Parameters here:

The second route might mean: “get comment 2 that belongs to post 13.”

Using Human-Friendly Identifiers (Slugs)

Not all parameters must be numeric. You can also use slugs or strings:

text
GET /articles/what-is-backend-development
GET /categories/web-development/articles

The parameter what-is-backend-development might be an article_slug that the backend looks up.

Multiple Parameters in the Same Path

You can have more than one parameter:

text
GET /users/{user_id}/orders/{order_id}
GET /teams/{team_id}/members/{member_id}

Example URLs:

text
GET /users/10/orders/5
GET /teams/3/members/7

Here, the route structure gives more context:

How URL Parameters Are Defined in Routing

In the “Routing” chapter you saw that the backend server matches incoming URLs to specific handler functions. URL parameters are part of this routing pattern.

Although different frameworks use slightly different syntax, they all share the same concept: a placeholder inside the path.

Pattern Based Route Definitions

Below is a pseudo style that looks similar to many backend frameworks:

python
# Pattern with a URL parameter
GET /users/{user_id}

The handler might look like:

python
def get_user(user_id):
    # user_id comes from the URL, like /users/42
    ...

When a request arrives:

If the path is /users, with no /{user_id}, this route does not match.

Matching Multiple Routes

You can define more than one route under the same base path:

text
GET /users               # list users
GET /users/{user_id}     # get a specific user

Example behavior:

The presence of a value in the {user_id} position is what makes the second route match.


Data Types of URL Parameters

Even though every URL is a string, many frameworks let you specify what type your URL parameter should have, for example integer, string, or UUID.

String Parameters

Default type is often string:

text
GET /articles/{slug}

Example:

text
GET /articles/hello-world

The string hello-world becomes the slug parameter in the handler.

Integer Parameters

Integers are common for IDs:

text
GET /users/{user_id}

Example:

text
GET /users/123

The parameter user_id has the value 123, but in code it is treated as an integer instead of a string.

If a framework expects an integer and the URL contains text like /users/abc, the framework will usually reject the request, often with a 404 Not Found or 422 Unprocessable Entity.

UUID Parameters

Some APIs use UUIDs instead of numeric IDs:

text
GET /resources/{resource_id}

Example:

text
GET /resources/550e8400-e29b-41d4-a716-446655440000

The parameter resource_id is a UUID. The route pattern tells the framework to parse it as such.

Important statement:
Even though URL parameters are strings in the URL, frameworks can parse and validate them as typed values such as integers or UUIDs.


URL Encoding in Parameters

URL paths can only safely contain a limited set of characters. If you need to use characters like spaces, ?, #, /, and others, the browser encodes them.

This process is called URL encoding or percent encoding.

Examples of Encoded Characters

CharacterEncoded form
space%20
/%2F
?%3F
#%23

If you have a parameter that might contain spaces, for example a username:

text
/users/John Doe

The actual request URL might become:

text
/users/John%20Doe

The framework will decode %20 back into a space. In your handler, you will receive John Doe.

When This Matters

Designing Clear Paths with URL Parameters

The way you design URL parameters affects how understandable your API is.

Use Nouns, Not Verbs, in Paths

Paths should represent resources, not actions. Use HTTP methods to represent actions.

Less clear:

text
GET /getUser/42
POST /createUser

Clearer:

text
GET /users/42
POST /users

/users/42 uses a URL parameter 42 to identify the user.

Put the Identifier in the URL Parameter Position

If you need to work with a single resource, put its identifier in the path:

You will see this pattern often in REST APIs.

Use Hierarchy for Nested Resources

Sometimes a resource is naturally dependent on another. For example, a comment belongs to a post.

Common patterns:

text
GET /posts/{post_id}/comments
GET /posts/{post_id}/comments/{comment_id}

This shows that the comment is tied to the post.


Error Cases and Edge Cases with URL Parameters

When using URL parameters, some common issues can appear.

Missing Parameters

If a route expects /users/{user_id}, then:

Example:

Requests:

Invalid Parameter Format

If a parameter is supposed to be an integer but the client sends a string that cannot be converted, such as /users/abc, there are two typical behaviors:

In both cases, the handler usually does not run because the route did not match correctly.

Resource Not Found

Sometimes the URL format is correct, but the resource does not exist. For example:

text
GET /users/9999

9999 is a valid integer. But if there is no user with ID 9999 in the database, your handler will typically return 404 Not Found.

It is important to distinguish:

Summary

URL parameters are variable parts of the URL path that let you identify specific resources. Typical patterns include:

They are:

You saw how they differ from query parameters and request bodies, how frameworks typically extract and validate them, and how to design clear, readable paths that use parameters effectively.

In the next chapters, you will use URL parameters together with query parameters and request bodies to build more complete endpoints.

Views: 9

Comments

Please login to add a comment.

Don't have an account? Register now!