HTTP traffic
HTTP traffic
Send requests, wait for them, and fail, mock or change them. Which keyword sees which request, and the rules they share for matchers, bodies and headers.
Browser has keywords to send HTTP requests, to wait for them and to change them:
| Keyword | Does | Page |
|---|---|---|
HTTP | Sends a request from the page, with its fetch | Sending requests |
Send API Request | Sends a request from the context, outside any page | Sending requests |
Wait For Request, Wait For Response | Wait for a request or response of the page | Waiting for traffic |
Abort Requests, Mock Responses, Modify Responses | Fail, answer or change requests of the page | Route rules |
Remove Route Rule, Remove All Route Rules | End route rules | Route rules |
Page traffic and API traffic
There are two kinds of requests, and they see different things:
| Page traffic, HTTP | Send API Request | |
|---|---|---|
| Route rules | apply | do not apply |
| Wait For Request / Response | see it | do not see it |
| Set Offline | requests fail | no effect |
| Context cookies | by the credentials option of fetch | always; cookies it gets reach the pages |
| Extra HTTP headers | sent | sent, but not those set on=Page |
| HTTP credentials | sent | sent |
| Relative URL | against the page URL | against baseURL, fails without one |
CORS, headers such as Cookie | browser rules apply | no rules |
| Needs | a page | a context |
So HTTP tests what the page can do, and Send API Request sets up or checks data
next to it, for example with the session the page logged in with.
Matchers
Wait For * and the route rules take the same matcher:
| Matcher | Example |
|---|---|
Glob: * stops at /, ** does not, {a,b} is either | **/api/orders/* |
JavaScript RegExp between slashes, with optional flags gimsuy | /\/api\/orders\/\d+$/i |
| JavaScript arrow function that returns a boolean | url => url.pathname.endsWith('.png') |
- An invalid RegExp fails the keyword. It never falls back to a glob.
- Anything that starts and ends with
/is a RegExp:/api/users/is one. Write**/api/users/for the glob. - The arrow function gets the request URL in a route rule, and the request or
response in
Wait For *. - Escape backslashes for Robot Framework:
\\dinstead of\d.
Bodies
body of HTTP, Mock Responses and Modify Responses, request_body of
Modify Responses and data of Send API Request take the same values:
| Value | Sent as |
|---|---|
| Dictionary, list, number, boolean | Compact JSON, application/json |
| Text | UTF-8, as given. The Content-Type differs per keyword, see its page |
bytes | Unchanged, byte for byte |
Robot Framework Secret | Its text, never logged |
A Content-Type in headers always wins. A value JSON cannot hold, such as NaN or
a date, fails before anything is sent.
Headers
Headers are a dictionary. A value is text, a Secret, a number or a boolean;
numbers and booleans are sent as JavaScript writes them, 5 and true. In
Modify Responses, None removes a header and "" sends it empty.
Responses
HTTP, Send API Request and Wait For Response return a dictionary with the same
core keys: status, statusText, ok, url, headers, bytes, text and
json. Each adds its own, see HttpResponse,
APIResponse and
CapturedResponse.
Secrets
Give tokens as a Robot Framework Secret, as a header value or as the whole body. The keywords log header names, never values. Two limits:
- A Secret inside text, such as
{"token": "${token}"}, becomes the text<secret>. Give the whole body as the Secret. - With
enable_playwright_debug, the values show in the Playwright debug log.