Working with Path and Query Parameters

Introduction to Path and Query Parameters

Welcome to another lesson of this course. In our previous lessons, we established a foundation by learning about RESTful APIs and making GET requests with the Requests-Scala library. Now, we will shift our focus to path and query parameters, essential tools for refining API requests and fetching specific data.

Path and query parameters play a crucial role in making your API requests more precise and efficient. Imagine you are shopping online: selecting a specific item using its ID is akin to a path parameter, while filtering items by categories like price or color resembles query parameters. In this lesson, we'll explore these concepts with practical examples, empowering you to extract just the information you need from an API.

Understanding Path Parameters

Path parameters are part of the URL used to access specific resources within an API, acting like unique identifiers. For example, if you want to retrieve a to-do item with ID 3, the URL would be structured as follows:

text
http://localhost:8000/todos/3

In this case, 3 is the path parameter specifying the particular item you wish to access.

Understanding Query Parameters

Query parameters are added to the URL after a ? to filter or modify the data returned by the API. They are formatted as key-value pairs. For instance, if you want to list only the completed to-do tasks, your request would be:

text
http://localhost:8000/todos?done=true

Here, done=true is the query parameter filtering the results to include only tasks that are marked as completed.

Setup Recap

Before jumping into implementing these parameters using Requests-Scala, ensure your setup is ready. Start by importing the necessary packages and defining the base URL:

Scala
import requests.*
import scala.util.{Failure, Success, Try}
import ujson.*

val baseUrl = "http://localhost:8000"

Now we're all set to dive into the code examples!

Fetching Data with Path Parameters

Path parameters are used to target specific resources within an API, allowing you to access individual items directly. They are appended directly to the endpoint URL. For instance, if you want to fetch details of a specific to-do item using its ID, you'd use a path parameter.

Here's a practical example using ID 3:

Scala
val todoId = 3

val response = Try(requests.get(s"$baseUrl/todos/$todoId"))

response match
  case Success(resp) if resp.statusCode == 200 =>
    val todo = ujson.read(resp.text())
    println(s"ID: ${todo("id")}")
    println(s"Title: ${todo("title")}")
    println(s"Description: ${todo("description")}")
    println(s"Done: ${todo("done")}")

  case Success(resp) =>
    println("Error fetching the todo with path parameter")
    println(s"Status Code: ${resp.statusCode}")
    println(s"Error Details: ${resp.text()}")

  case Failure(exception) =>
    println(s"HTTP request failed: ${exception.getMessage}")

In this example, the todoId is a path parameter specifying the to-do item with ID 3, and the full URL looks like http://localhost:8000/todos/3. If successful, you'll get an output displaying the task details:

text
ID: 3
Title: Finish project report
Description: Summarize Q4 performance metrics
Done: False

This output confirms that the specific item was retrieved, demonstrating how path parameters enable you to access individual resources accurately.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal