AgentSkillsCN

robotframework-requests-skill

指导 AI 代理使用 RequestsLibrary 编写 REST API 测试。当您需要构建 HTTP 客户端测试、进行 JSON/XML API 测试、管理会话、实现身份验证、上传文件,或验证响应内容时,可使用此技能。

SKILL.md
--- frontmatter
name: robotframework-requests-skill
description: Guide AI agents in creating REST API tests using RequestsLibrary. Use when building HTTP client tests, JSON/XML API testing, session management, authentication, file uploads, and response validation.

Requests Library Skill

Quick Reference

RequestsLibrary provides HTTP client keywords for REST API testing, wrapping Python's requests library. It supports JSON, XML, form data, file uploads, and all authentication methods.

Installation

bash
pip install robotframework-requests

Library Import

robotframework
*** Settings ***
Library    RequestsLibrary
Library    Collections    # Often needed for dict/list operations

Two Usage Styles

Style 1: Sessionless (Simple, Recommended for Most Cases)

Direct HTTP calls without session management:

robotframework
${response}=    GET    https://api.example.com/users
${response}=    POST   https://api.example.com/users    json=${data}
${response}=    PUT    https://api.example.com/users/1  json=${data}
${response}=    DELETE https://api.example.com/users/1

Style 2: With Session (For Multiple Calls to Same API)

Create a session once, reuse for multiple requests:

robotframework
Create Session    api    https://api.example.com    verify=${True}
${response}=    GET On Session    api    /users
${response}=    POST On Session   api    /users    json=${data}
${response}=    DELETE On Session    api    /users/1

Core Keywords Quick Reference

HTTP Methods

KeywordUsageDescription
GETGET ${URL}Retrieve resource
POSTPOST ${URL} json=${data}Create resource
PUTPUT ${URL} json=${data}Replace resource
PATCHPATCH ${URL} json=${data}Partial update
DELETEDELETE ${URL}Remove resource
HEADHEAD ${URL}Get headers only
OPTIONSOPTIONS ${URL}Get allowed methods

Request Options

OptionExampleDescription
jsonjson=${dict}Send JSON body
datadata=${form}Send form data
paramsparams=${query}URL query parameters
headersheaders=${headers}Custom headers
expected_statusexpected_status=201Verify status code
timeouttimeout=30Request timeout (seconds)
verifyverify=${False}SSL verification

Working with JSON

Send JSON Data

robotframework
&{user}=    Create Dictionary    name=John    email=john@example.com
${response}=    POST    ${API_URL}/users    json=${user}

Parse JSON Response

robotframework
${response}=    GET    ${API_URL}/users/1
${json}=    Set Variable    ${response.json()}
${name}=    Set Variable    ${json}[name]
${email}=   Set Variable    ${json}[email]

Access Nested JSON

robotframework
# Response: {"user": {"profile": {"name": "John"}}}
${name}=    Set Variable    ${response.json()}[user][profile][name]

Response Validation

Status Code Verification

robotframework
# In request (recommended)
${response}=    GET    ${URL}    expected_status=200
${response}=    POST   ${URL}    json=${data}    expected_status=201
${response}=    DELETE ${URL}    expected_status=204

# Post-request
Status Should Be    200    ${response}
Should Be Equal As Integers    ${response.status_code}    200

# Accept any status (for error testing)
${response}=    GET    ${URL}/notfound    expected_status=anything

Response Content Validation

robotframework
Should Be Equal    ${response.json()}[status]    success
Should Contain     ${response.text}    success
Dictionary Should Contain Key    ${response.json()}    id
Should Not Be Empty    ${response.json()}[name]

Headers

Set Request Headers

robotframework
&{headers}=    Create Dictionary
...    Authorization=Bearer ${TOKEN}
...    Content-Type=application/json
...    Accept=application/json
${response}=    GET    ${URL}    headers=${headers}

Check Response Headers

robotframework
${content_type}=    Set Variable    ${response.headers}[Content-Type]
Should Contain    ${content_type}    application/json

Common Patterns

CRUD Operations

robotframework
*** Test Cases ***
CRUD User Lifecycle
    # Create
    &{user}=    Create Dictionary    name=John    email=john@test.com
    ${response}=    POST    ${API}/users    json=${user}    expected_status=201
    ${user_id}=    Set Variable    ${response.json()}[id]

    # Read
    ${response}=    GET    ${API}/users/${user_id}    expected_status=200
    Should Be Equal    ${response.json()}[name]    John

    # Update
    &{updates}=    Create Dictionary    name=John Updated
    ${response}=    PUT    ${API}/users/${user_id}    json=${updates}    expected_status=200
    Should Be Equal    ${response.json()}[name]    John Updated

    # Delete
    ${response}=    DELETE    ${API}/users/${user_id}    expected_status=204

Authentication Patterns

robotframework
# Bearer Token
&{headers}=    Create Dictionary    Authorization=Bearer ${TOKEN}
${response}=    GET    ${URL}    headers=${headers}

# Basic Auth (using auth parameter)
${auth}=    Create List    ${USERNAME}    ${PASSWORD}
${response}=    GET    ${URL}    auth=${auth}

Query Parameters

robotframework
&{params}=    Create Dictionary    page=1    limit=10    sort=name
${response}=    GET    ${API}/users    params=${params}
# Results in: GET /users?page=1&limit=10&sort=name

Response Object Properties

PropertyDescriptionExample
status_codeHTTP status code${response.status_code}
textResponse body as text${response.text}
json()Parse JSON response${response.json()}
headersResponse headers dict${response.headers}[Content-Type]
contentResponse body as bytes${response.content}
cookiesResponse cookies${response.cookies}
elapsedRequest duration${response.elapsed.total_seconds()}

When to Load Additional References

Load these reference files for specific use cases:

  • Session management with cookies/state -> references/sessions.md
  • All HTTP methods and options -> references/request-methods.md
  • Complex JSON payloads/parsing -> references/json-handling.md
  • XML/SOAP APIs -> references/xml-text-handling.md
  • Response assertions -> references/response-validation.md
  • OAuth, JWT, API keys -> references/authentication.md
  • File upload/download -> references/files-upload-download.md
  • SSL certificates, mTLS -> references/ssl-certificates.md
  • Error debugging -> references/troubleshooting.md