Skip to content

Merge repeated curl -d/--data options in Request.from_curl() - #7728

Merged
AdrianAtZyte merged 1 commit into
scrapy:masterfrom
winklemad:fix/curl-multiple-data-flags
Jul 20, 2026
Merged

AdrianAtZyte merged 1 commit into
scrapy:masterfrom
winklemad:fix/curl-multiple-data-flags

Conversation

@winklemad

Copy link
Copy Markdown
Contributor

No linked issue — found while reading scrapy/utils/curl.py.

Summary

Request.from_curl() silently drops all but the last -d/--data/--data-raw value when a curl command repeats them.

curl merges repeated data options into a single body joined with &. From the curl man page (-d, --data):

If any of these options is used more than once on the same command line, the data pieces specified are merged with a separating &-symbol. Thus, using -d name=daniel -d skill=lousy would generate a post chunk that looks like name=daniel&skill=lousy.

But scrapy's DataAction uses a single-value action, so it keeps only the last one:

>>> from scrapy.utils.curl import curl_to_request_kwargs
>>> curl_to_request_kwargs("curl -d 'a=1' -d 'b=2' https://example.org")["body"]
'b=2'          # 'a=1' is lost; real curl sends 'a=1&b=2'

Verified against real curl 7.87. This bites anyone converting a hand-written or tool-generated curl command with multiple -d flags into a Scrapy request.

Fix

Accumulate repeated values with & in DataAction, mirroring curl. This covers the whole class: -H/--header and -b/--cookie already accumulate via action="append"; -d/--data/--data-raw (all mapped to DataAction) was the only repeatable data flag that didn't.

>>> curl_to_request_kwargs("curl -d 'a=1' -d 'b=2' https://example.org")["body"]
'a=1&b=2'

The per-value $-prefix stripping (from $'...' shell quoting) still applies to each option before the merge.

Tests

Adds three cases to tests/test_utils_curl.py — repeated -d, mixed -d/--data/--data-raw, and repeated with $-prefix quoting. They fail on the current code and pass with the fix; the full test_utils_curl.py suite passes and ruff check + format are clean.

curl merges repeated -d/--data/--data-raw options into a single request
body joined with "&" (e.g. `curl -d a=1 -d b=2` sends `a=1&b=2`). scrapy's
DataAction kept only the last value, silently dropping the earlier ones, so
`Request.from_curl("curl -d a=1 -d b=2 ...")` produced a body of just `b=2`.
Accumulate the values with "&" to match curl. -H/--header and -b/--cookie
already accumulate via action="append"; -d was the only repeatable data flag
that didn't.
@codecov

codecov Bot commented Jul 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.31%. Comparing base (c944693) to head (8ec0347).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7728   +/-   ##
=======================================
  Coverage   95.31%   95.31%           
=======================================
  Files         164      164           
  Lines       13023    13026    +3     
  Branches     1688     1689    +1     
=======================================
+ Hits        12413    12416    +3     
  Misses        419      419           
  Partials      191      191           
Files with missing lines Coverage Δ
scrapy/utils/curl.py 97.36% <100.00%> (+0.10%) ⬆️

@AdrianAtZyte
AdrianAtZyte merged commit 7faf20c into scrapy:master Jul 20, 2026
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants