Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/crawlee/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ def __call__(
url: str,
*,
method: HttpMethod = 'GET',
payload: HttpPayload | None = None,
headers: HttpHeaders | dict[str, str] | None = None,
) -> Coroutine[None, None, HttpResponse]:
"""Call send request function.
Expand All @@ -556,6 +557,7 @@ def __call__(
url: The URL to send the request to.
method: The HTTP method to use.
headers: The headers to include in the request.
payload: The payload to include in the request.

Returns:
The HTTP response received from the server.
Expand Down
3 changes: 3 additions & 0 deletions src/crawlee/crawlers/_basic/_basic_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
BasicCrawlingContext,
GetKeyValueStoreFromRequestHandlerFunction,
HttpHeaders,
HttpPayload,
RequestHandlerRunResult,
SendRequestFunction,
SkippedReason,
Expand Down Expand Up @@ -1081,11 +1082,13 @@ async def send_request(
url: str,
*,
method: HttpMethod = 'GET',
payload: HttpPayload | None = None,
headers: HttpHeaders | dict[str, str] | None = None,
) -> HttpResponse:
return await self._http_client.send_request(
url=url,
method=method,
payload=payload,
headers=headers,
session=session,
proxy_info=proxy_info,
Expand Down
19 changes: 13 additions & 6 deletions tests/unit/crawlers/_basic/test_basic_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from crawlee import ConcurrencySettings, Glob, service_locator
from crawlee._request import Request
from crawlee._types import BasicCrawlingContext, EnqueueLinksKwargs, HttpHeaders
from crawlee._types import BasicCrawlingContext, EnqueueLinksKwargs, HttpHeaders, HttpMethod
from crawlee._utils.robots import RobotsTxtFile
from crawlee.configuration import Configuration
from crawlee.crawlers import BasicCrawler
Expand Down Expand Up @@ -300,29 +300,36 @@ async def failed_request_handler(context: BasicCrawlingContext, error: Exception
await crawler.run(['http://a.com/', 'http://b.com/', 'http://c.com/'])


async def test_send_request_works(server_url: URL) -> None:
@pytest.mark.parametrize(
('method', 'path', 'payload'),
[
pytest.param('GET', 'get', None, id='get send_request'),
pytest.param('POST', 'post', 'Hello, world!', id='post send_request'),
],
)
async def test_send_request_works(server_url: URL, method: HttpMethod, path: str, payload: None | bytes) -> None:
Copy link

Copilot AI May 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The POST test case is providing a payload as a string ('Hello, world!'), while the function signature indicates a payload of type 'None | bytes'. Ensure type consistency by either converting the string to bytes or updating the type annotation if string payloads are supported.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

response_data: dict[str, Any] = {}

crawler = BasicCrawler(max_request_retries=3)

@crawler.router.default_handler
async def handler(context: BasicCrawlingContext) -> None:
response = await context.send_request(str(server_url))
response = await context.send_request(str(server_url / path), method=method, payload=payload)

response_data['body'] = response.read()
response_data['body'] = json.loads(response.read())
response_data['headers'] = response.headers

await crawler.run(['http://a.com/', 'http://b.com/', 'http://c.com/'])

response_body = response_data.get('body')
assert response_body is not None
assert b'Hello, world!' in response_body
assert response_body.get('data') == payload

response_headers = response_data.get('headers')
assert response_headers is not None
content_type = response_headers.get('content-type')
assert content_type is not None
assert content_type == 'text/html; charset=utf-8'
assert content_type == 'application/json'


@dataclass
Expand Down
Loading