|
| 1 | +# Fetch Commands |
| 2 | + |
| 3 | +Fetch commands provide advanced network request handling and interception capabilities using the Fetch API domain. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The fetch commands module enables sophisticated network request management, including request modification, response interception, and authentication handling. |
| 8 | + |
| 9 | +::: pydoll.commands.fetch_commands |
| 10 | + options: |
| 11 | + show_root_heading: true |
| 12 | + show_source: false |
| 13 | + heading_level: 2 |
| 14 | + filters: |
| 15 | + - "!^_" |
| 16 | + - "!^__" |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +Fetch commands are used for advanced network interception and request handling: |
| 21 | + |
| 22 | +```python |
| 23 | +from pydoll.commands.fetch_commands import enable, request_paused, continue_request |
| 24 | +from pydoll.connection.connection_handler import ConnectionHandler |
| 25 | + |
| 26 | +# Enable fetch domain |
| 27 | +connection = ConnectionHandler() |
| 28 | +await enable(connection, patterns=[{ |
| 29 | + "urlPattern": "*", |
| 30 | + "requestStage": "Request" |
| 31 | +}]) |
| 32 | + |
| 33 | +# Handle paused requests |
| 34 | +async def handle_paused_request(request_id, request): |
| 35 | + # Modify request or continue as-is |
| 36 | + await continue_request(connection, request_id=request_id) |
| 37 | +``` |
| 38 | + |
| 39 | +## Key Functionality |
| 40 | + |
| 41 | +The fetch commands module provides functions for: |
| 42 | + |
| 43 | +### Request Interception |
| 44 | +- `enable()` - Enable fetch domain with patterns |
| 45 | +- `disable()` - Disable fetch domain |
| 46 | +- `continue_request()` - Continue intercepted requests |
| 47 | +- `fail_request()` - Fail requests with specific errors |
| 48 | + |
| 49 | +### Request Modification |
| 50 | +- Modify request headers |
| 51 | +- Change request URLs |
| 52 | +- Alter request methods (GET, POST, etc.) |
| 53 | +- Modify request bodies |
| 54 | + |
| 55 | +### Response Handling |
| 56 | +- `fulfill_request()` - Provide custom responses |
| 57 | +- `get_response_body()` - Get response content |
| 58 | +- Response header modification |
| 59 | +- Response status code control |
| 60 | + |
| 61 | +### Authentication |
| 62 | +- `continue_with_auth()` - Handle authentication challenges |
| 63 | +- Basic authentication support |
| 64 | +- Custom authentication flows |
| 65 | + |
| 66 | +## Advanced Features |
| 67 | + |
| 68 | +### Pattern-Based Interception |
| 69 | +```python |
| 70 | +# Intercept specific URL patterns |
| 71 | +patterns = [ |
| 72 | + {"urlPattern": "*/api/*", "requestStage": "Request"}, |
| 73 | + {"urlPattern": "*.js", "requestStage": "Response"}, |
| 74 | + {"urlPattern": "https://example.com/*", "requestStage": "Request"} |
| 75 | +] |
| 76 | + |
| 77 | +await enable(connection, patterns=patterns) |
| 78 | +``` |
| 79 | + |
| 80 | +### Request Modification |
| 81 | +```python |
| 82 | +# Modify intercepted requests |
| 83 | +async def modify_request(request_id, request): |
| 84 | + # Add authentication header |
| 85 | + headers = request.headers.copy() |
| 86 | + headers["Authorization"] = "Bearer token123" |
| 87 | + |
| 88 | + # Continue with modified headers |
| 89 | + await continue_request( |
| 90 | + connection, |
| 91 | + request_id=request_id, |
| 92 | + headers=headers |
| 93 | + ) |
| 94 | +``` |
| 95 | + |
| 96 | +### Response Mocking |
| 97 | +```python |
| 98 | +# Mock API responses |
| 99 | +await fulfill_request( |
| 100 | + connection, |
| 101 | + request_id=request_id, |
| 102 | + response_code=200, |
| 103 | + response_headers=[ |
| 104 | + {"name": "Content-Type", "value": "application/json"}, |
| 105 | + {"name": "Access-Control-Allow-Origin", "value": "*"} |
| 106 | + ], |
| 107 | + body='{"status": "success", "data": {"mocked": true}}' |
| 108 | +) |
| 109 | +``` |
| 110 | + |
| 111 | +### Authentication Handling |
| 112 | +```python |
| 113 | +# Handle authentication challenges |
| 114 | +await continue_with_auth( |
| 115 | + connection, |
| 116 | + request_id=request_id, |
| 117 | + auth_challenge_response={ |
| 118 | + "response": "ProvideCredentials", |
| 119 | + "username": "user", |
| 120 | + "password": "pass" |
| 121 | + } |
| 122 | +) |
| 123 | +``` |
| 124 | + |
| 125 | +## Request Stages |
| 126 | + |
| 127 | +Fetch commands can intercept requests at different stages: |
| 128 | + |
| 129 | +| Stage | Description | Use Cases | |
| 130 | +|-------|-------------|-----------| |
| 131 | +| Request | Before request is sent | Modify headers, URL, method | |
| 132 | +| Response | After response received | Mock responses, modify content | |
| 133 | + |
| 134 | +## Error Handling |
| 135 | + |
| 136 | +```python |
| 137 | +# Fail requests with specific errors |
| 138 | +await fail_request( |
| 139 | + connection, |
| 140 | + request_id=request_id, |
| 141 | + error_reason="ConnectionRefused" # or "AccessDenied", "TimedOut", etc. |
| 142 | +) |
| 143 | +``` |
| 144 | + |
| 145 | +## Integration with Network Commands |
| 146 | + |
| 147 | +Fetch commands work alongside network commands but provide more granular control: |
| 148 | + |
| 149 | +- **Network Commands**: Broader network monitoring and control |
| 150 | +- **Fetch Commands**: Specific request/response interception and modification |
| 151 | + |
| 152 | +!!! tip "Performance Considerations" |
| 153 | + Fetch interception can impact page loading performance. Use specific URL patterns and disable when not needed to minimize overhead. |
0 commit comments