Skip to content

Commit 19f5add

Browse files
authored
fix: Accept string payload in the Request constructor (#697)
- This is a follow-up to #668 (comment). - Let's accept `payload` as both str and bytes in the `Request.from_url` constructor. - The field `Request.payload` has a validator that accepts both. With this change, we are consistent in the `from_url` constructor as well. - We do a similar thing for the `Request.headers` as well.
1 parent b703709 commit 19f5add

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/crawlee/_request.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def from_url(
183183
*,
184184
method: HttpMethod = 'GET',
185185
headers: HttpHeaders | dict[str, str] | None = None,
186-
payload: HttpPayload | None = None,
186+
payload: HttpPayload | str | None = None,
187187
label: str | None = None,
188188
unique_key: str | None = None,
189189
id: str | None = None,
@@ -195,6 +195,9 @@ def from_url(
195195
if isinstance(headers, dict) or headers is None:
196196
headers = HttpHeaders(headers or {})
197197

198+
if isinstance(payload, str):
199+
payload = payload.encode()
200+
198201
unique_key = unique_key or compute_unique_key(
199202
url,
200203
method=method,
@@ -276,7 +279,7 @@ def from_url(
276279
*,
277280
method: HttpMethod = 'GET',
278281
headers: HttpHeaders | dict[str, str] | None = None,
279-
payload: HttpPayload | None = None,
282+
payload: HttpPayload | str | None = None,
280283
label: str | None = None,
281284
unique_key: str | None = None,
282285
id: str | None = None,
@@ -317,6 +320,9 @@ def from_url(
317320
if isinstance(headers, dict) or headers is None:
318321
headers = HttpHeaders(headers or {})
319322

323+
if isinstance(payload, str):
324+
payload = payload.encode()
325+
320326
unique_key = unique_key or compute_unique_key(
321327
url,
322328
method=method,

0 commit comments

Comments
 (0)