Implementation:MarketSquare Robotframework browser Network Http
Network Http
API documentation for the HTTP keyword that performs HTTP requests within a browser context.
Overview
The HTTP keyword is defined in the Network class and provides the ability to make HTTP requests directly from within a Playwright browser page using the browser's native fetch() API. It supports all standard HTTP methods and returns a structured response object.
Source Location
| Component | File | Lines |
|---|---|---|
http keyword (Python) |
Browser/keywords/network.py |
L58-109 |
httpRequest (Node.js) |
node/playwright-wrapper/network.ts |
L26-54 |
_get_headers helper |
Browser/keywords/network.py |
L26-31 |
_format_response helper |
Browser/keywords/network.py |
L34-40 |
Keyword Signature
@keyword(tags=("HTTP",))
def http(
self,
url: str,
method: RequestMethod = RequestMethod.GET,
body: str | None = None,
headers: dict | None = None,
) -> Any:
Robot Framework keyword name: HTTP
Tags: HTTP
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
url |
str |
(required) | The request URL. Can be a relative path (e.g., /api/foo) if the context was created with a baseURL, or an absolute URL.
|
method |
RequestMethod |
GET |
The HTTP method. Supported values: GET, POST, PUT, DELETE, PATCH, HEAD.
|
body |
None | None |
The request body as a string. GET requests cannot have a body (the Node-side implementation skips the body for GET). If the body is valid JSON, the Content-Type header is automatically set to application/json.
|
headers |
None | None |
A dictionary of additional request headers. Merged with any auto-detected Content-Type header. |
Return Value
Returns a DotDict (from robot.utils) with the following attributes:
| Attribute | Type | Description |
|---|---|---|
status |
int |
HTTP status code (e.g., 200, 404, 500). |
statusText |
str |
Human-readable status text (e.g., "OK"). May not be available in all browsers. |
body |
str | Response body. Auto-parsed as a Python dictionary when the response Content-Type is application/json; otherwise returned as a string.
|
headers |
dict |
Dictionary of all response headers. |
ok |
bool |
True if status is in the 200-299 range.
|
url |
str |
The final URL after redirects. |
redirected |
bool |
Whether the request was redirected. |
type |
str |
Response type (e.g., "basic", "cors"). |
If the response cannot be converted to a DotDict (e.g., the response is not a dictionary), the raw response is returned instead and a debug log message is emitted.
Usage Examples
Simple GET request:
*** Test Cases ***
Get Users
New Browser chromium headless=True
New Context baseURL=http://localhost:8080
New Page
&{resp}= HTTP /api/users
Should Be Equal As Integers ${resp.status} 200
Should Be True ${resp.ok}
POST request with JSON body:
*** Test Cases ***
Create User
New Browser chromium headless=True
New Context baseURL=http://localhost:8080
New Page
${body}= Set Variable {"name": "John", "email": "john@example.com"}
&{resp}= HTTP /api/users method=POST body=${body}
Should Be Equal As Integers ${resp.status} 201
Should Be Equal ${resp.body.name} John
Request with custom headers:
*** Test Cases ***
Get With Custom Headers
New Browser chromium headless=True
New Context baseURL=http://localhost:8080
New Page
${headers}= Create Dictionary X-API-Key=abc123 Accept=application/json
&{resp}= HTTP /api/data headers=${headers}
Should Be Equal As Integers ${resp.status} 200
DELETE request:
*** Test Cases ***
Delete User
New Browser chromium headless=True
New Context baseURL=http://localhost:8080
New Page
&{resp}= HTTP /api/users/42 method=DELETE
Should Be Equal As Integers ${resp.status} 204
Internal Execution Flow
The HTTP keyword follows this execution path:
- Header preparation: The
_get_headers(body, headers)helper at L26-31 attempts to parse the body as JSON. If successful, it merges{"Content-Type": "application/json"}with the caller-provided headers. If parsing fails (not JSON), only the caller-provided headers are used.
- gRPC dispatch: The keyword opens a gRPC channel and sends an
HttpRequestmessage to the Node.js process:
response = stub.HttpRequest(
Request().HttpRequest(
url=url,
method=method.name if method else "GET",
body=body,
headers=json.dumps(_get_headers(body, headers)),
)
)
- Node-side execution (network.ts L26-54): The Node process constructs a
fetch()call and executes it within the page context viapage.evaluate():
const response = await page.evaluate(({ url, method, body, headers }) => {
return fetch(url, { method, body, headers }).then((data: Response) => {
return data.text().then((body) => {
const headers: { [k: string]: any } = {};
data.headers.forEach((value, name) => (headers[name] = value));
return {
status: data.status,
body: body,
headers: JSON.stringify(headers),
type: data.type,
statusText: data.statusText,
url: data.url,
ok: data.ok,
redirected: data.redirected,
};
});
});
}, opts);
- Response formatting: The
_format_response()helper at L34-40 processes the raw JSON response, converting header strings to dictionaries and auto-parsing JSON response bodies via_jsonize_content().
- DotDict wrapping: The formatted response dictionary is wrapped in a
DotDictfor Robot Framework dot-notation access.
Important Notes
- A page must exist in the context before calling
HTTP. The keyword usespage.evaluate()internally, which requires an active page. - The body parameter for GET requests is ignored on the Node side. The TypeScript code explicitly checks
if (opts.method != 'GET')before including the body. - The request executes within the page's JavaScript security context, so CORS restrictions apply.
- Empty body strings are treated as no body:
body = body if body else "".