Skip to content

Requests and GET

Chapter lab: API foundationsch11-api-foundations.zip · 17 KB

This chapter works in a prepared project. Download it before the first task; it unpacks into its own folder.

Download ch11-api-foundations.zip

Or fetch and unpack it from a terminal in VS Code:

Terminal window
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch11-api-foundations.zip && unzip -q ch11-api-foundations.zip && rm ch11-api-foundations.zip

Open the folder in VS Code with code rfe-ch11-api-foundations (or File → Open Folder…) and accept the recommended RobotCode and Python extensions. Then set it up:

Terminal window
uv sync
Terminal window
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Keep the environment active in every terminal where you run lesson commands.

Terminal window
poetry install --no-root

Python 3.13 or newer is required; uv installs it when it is missing.

Also needed for this lab

Start the RF Demo Shop in its own terminal and keep it running while you work:

Terminal window
uv run python demo_app/serve.py
Terminal window
python demo_app/serve.py
Terminal window
poetry run python demo_app/serve.py

Open in browserNo setup? Try the lab right here: its files, an editor and a terminal, running in your browser.

What's inside
  • Directoryrfe-ch11-api-foundations/
    • Directory.vscode/
      • extensions.json
    • Directorychecks/
      • smoke.robot
    • Directorydemo_app/
      • api.py
      • serve.py
      • store.py
      • web.py
    • Directoryresources/
      • api.resource
    • .gitignore
    • .python-version
    • LAB.md
    • pyproject.toml
    • README.md
    • requirements.txt
    • uv.lock

The RF Demo Shop answers on a JSON API as well as a web page. Testing the API directly is fast and checks the contract at the seam — no browser needed.

The RequestsLibrary provides the HTTP keywords. GET <url> fetches a resource and returns a response you can assert on.

Task 1 — Start the RF Demo Shop and keep it running

Section titled “Task 1 — Start the RF Demo Shop and keep it running”
Run

Start the shop in a terminal and leave it running:

Terminal window
uv run python demo_app/serve.py
Terminal window
python demo_app/serve.py
Terminal window
poetry run python demo_app/serve.py

Its API is at http://localhost:7273.

Done when

the API answers on its port.

Task 2 — GET the product catalog and check the count

Section titled “Task 2 — GET the product catalog and check the count”
Edit

Goal: create checks/get.robot that GETs the product catalog, asserts a 200, and checks how many products come back.

Here is the same GET-then-assert shape against a different endpoint. Adapt it to the catalog. (../resources/api.resource gives you ${API_URL}.)

Worked example — adapt it, don’t copy it

*** Settings ***
Library RequestsLibrary
Resource ../resources/api.resource
*** Test Cases ***
One Product Responds
${resp}= GET ${API_URL}/api/products/travel-mug
Status Should Be 200 ${resp}
Should Be Equal ${resp.json()}[slug] travel-mug

Done when

the GET returns 200 and four products.

Hint 1

The catalog lives at ${API_URL}/api/products — GET it, then Status Should Be 200 ${resp}.

Hint 2

The body is a JSON list, so assert its size with Length Should Be ${resp.json()} 4.

Solution
*** Settings ***
Library RequestsLibrary
Resource ../resources/api.resource
*** Test Cases ***
Catalog Lists Products
${resp}= GET ${API_URL}/api/products
Status Should Be 200 ${resp}
Length Should Be ${resp.json()} 4

Done when

the GET returns 200 and four products.

Check your understanding

Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.

  1. Which library drives HTTP requests in Robot Framework?

    Show the answer

    Answer: RequestsLibrary — RequestsLibrary (robotframework-requests) provides GET/POST and response assertions.

  2. Why test the API directly instead of only through the UI?

    Show the answer

    Answer: It is faster and checks the contract at the seam — API tests are fast and verify the contract directly; they complement, not replace, UI tests.

Key terms

RequestsLibraryconcept
HTTP keywords for Robot Framework: GET, POST, Status Should Be.
GETkeyword
${resp}= GET ${API_URL}/api/products — fetch a resource.

All terms in the glossary →