|
| 1 | +# Browser Requests |
| 2 | + |
| 3 | +The requests module provides HTTP request capabilities within the browser context, enabling seamless API calls that inherit the browser's session state, cookies, and authentication. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The browser requests module offers a `requests`-like interface for making HTTP calls directly within the browser's JavaScript context. This approach provides several advantages over traditional HTTP libraries: |
| 8 | + |
| 9 | +- **Session inheritance**: Automatic cookie, authentication, and CORS handling |
| 10 | +- **Browser context**: Requests execute in the same security context as the page |
| 11 | +- **No session juggling**: Eliminate the need to transfer cookies and tokens between automation and API calls |
| 12 | +- **SPA compatibility**: Perfect for Single Page Applications with complex authentication flows |
| 13 | + |
| 14 | +## Request Class |
| 15 | + |
| 16 | +The main interface for making HTTP requests within the browser context. |
| 17 | + |
| 18 | +::: pydoll.browser.requests.request.Request |
| 19 | + options: |
| 20 | + show_root_heading: true |
| 21 | + show_source: false |
| 22 | + heading_level: 3 |
| 23 | + group_by_category: true |
| 24 | + members_order: source |
| 25 | + filters: |
| 26 | + - "!^__" |
| 27 | + |
| 28 | +## Response Class |
| 29 | + |
| 30 | +Represents the response from HTTP requests, providing a familiar interface similar to the `requests` library. |
| 31 | + |
| 32 | +::: pydoll.browser.requests.response.Response |
| 33 | + options: |
| 34 | + show_root_heading: true |
| 35 | + show_source: false |
| 36 | + heading_level: 3 |
| 37 | + group_by_category: true |
| 38 | + members_order: source |
| 39 | + filters: |
| 40 | + - "!^__" |
| 41 | + |
| 42 | +## Usage Examples |
| 43 | + |
| 44 | +### Basic HTTP Methods |
| 45 | + |
| 46 | +```python |
| 47 | +from pydoll.browser.chromium import Chrome |
| 48 | + |
| 49 | +async with Chrome() as browser: |
| 50 | + tab = await browser.start() |
| 51 | + await tab.go_to("https://api.example.com") |
| 52 | + |
| 53 | + # GET request |
| 54 | + response = await tab.request.get("/users/123") |
| 55 | + user_data = await response.json() |
| 56 | + |
| 57 | + # POST request |
| 58 | + response = await tab.request.post("/users", json={ |
| 59 | + "name": "John Doe", |
| 60 | + |
| 61 | + }) |
| 62 | + |
| 63 | + # PUT request with headers |
| 64 | + response = await tab.request.put("/users/123", |
| 65 | + json={"name": "Jane Doe"}, |
| 66 | + headers={"Authorization": "Bearer token123"} |
| 67 | + ) |
| 68 | +``` |
| 69 | + |
| 70 | +### Response Handling |
| 71 | + |
| 72 | +```python |
| 73 | +# Check response status |
| 74 | +if response.ok: |
| 75 | + print(f"Success: {response.status_code}") |
| 76 | +else: |
| 77 | + print(f"Error: {response.status_code}") |
| 78 | + response.raise_for_status() # Raises HTTPError for 4xx/5xx |
| 79 | + |
| 80 | +# Access response data |
| 81 | +text_data = response.text |
| 82 | +json_data = await response.json() |
| 83 | +raw_bytes = response.content |
| 84 | + |
| 85 | +# Inspect headers and cookies |
| 86 | +print("Response headers:", response.headers) |
| 87 | +print("Request headers:", response.request_headers) |
| 88 | +for cookie in response.cookies: |
| 89 | + print(f"Cookie: {cookie.name}={cookie.value}") |
| 90 | +``` |
| 91 | + |
| 92 | +### Advanced Features |
| 93 | + |
| 94 | +```python |
| 95 | +# Request with custom headers and parameters |
| 96 | +response = await tab.request.get("/search", |
| 97 | + params={"q": "python", "limit": 10}, |
| 98 | + headers={ |
| 99 | + "User-Agent": "Custom Bot 1.0", |
| 100 | + "Accept": "application/json" |
| 101 | + } |
| 102 | +) |
| 103 | + |
| 104 | +# File upload simulation |
| 105 | +response = await tab.request.post("/upload", |
| 106 | + data={"description": "Test file"}, |
| 107 | + files={"file": ("test.txt", "file content", "text/plain")} |
| 108 | +) |
| 109 | + |
| 110 | +# Form data submission |
| 111 | +response = await tab.request.post("/login", |
| 112 | + data={"username": "user", "password": "pass"} |
| 113 | +) |
| 114 | +``` |
| 115 | + |
| 116 | +## Integration with Tab |
| 117 | + |
| 118 | +The request functionality is accessed through the `tab.request` property, which provides a singleton `Request` instance for each tab: |
| 119 | + |
| 120 | +```python |
| 121 | +# Each tab has its own request instance |
| 122 | +tab1 = await browser.get_tab(0) |
| 123 | +tab2 = await browser.new_tab() |
| 124 | + |
| 125 | +# These are separate Request instances |
| 126 | +request1 = tab1.request # Request bound to tab1 |
| 127 | +request2 = tab2.request # Request bound to tab2 |
| 128 | + |
| 129 | +# Requests inherit the tab's context |
| 130 | +await tab1.go_to("https://site1.com") |
| 131 | +await tab2.go_to("https://site2.com") |
| 132 | + |
| 133 | +# These requests will have different cookie/session contexts |
| 134 | +response1 = await tab1.request.get("/api/data") # Uses site1.com cookies |
| 135 | +response2 = await tab2.request.get("/api/data") # Uses site2.com cookies |
| 136 | +``` |
| 137 | + |
| 138 | +!!! tip "Hybrid Automation" |
| 139 | + This module is particularly powerful for hybrid automation scenarios where you need to combine UI interactions with API calls. For example, log in through the UI, then use the authenticated session for API calls without manually handling cookies or tokens. |
0 commit comments