New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape quotes in headers when building cURLRepresentation #2474

merged 1 commit into from Mar 31, 2018


None yet
2 participants

jonah-williams commented Mar 30, 2018

Goals ⚽️

I'm working on an app that found it useful to pass some structured data in an HTTP header by serializing a dictionary to JSON. We also log the curl equivalent of our applications requests in debug builds so that we can easily reply a request of interest.

We found that the logged curl commands failed to escape quotes in the header so our logged curl command would read as:

$ curl -x \
    -H "X-Custom-Header: {"key": "value"}" \

curl actually accepts and parses this, stripping the extra quotes from this header field:

> User-Agent: curl/7.54.0
> Accept: */*
> X-Custom-Header: {key: value}

Unfortunately this header string ({key: value}) is no longer parsable JSON.

Now cURLRepresentation will escape a single level of escaped quotes in headers resulting in:

$ curl -x \
    -H "X-Custom-Header: {\"key\": \"value\"}" \
> User-Agent: curl/7.54.0
> Accept: */*
> X-Custom-Header: {"key": "value"}

Implementation Details 🚧

Hopefully not a breaking change for anyone since it changes only the debug description of the request.

This won't cover all possible escaped characters in header strings. It possible someone could want to output a command with multiple levels of nested quotes. However since already only considers two levels of escaped quotes I think this is sufficient as an incremental improvement. A better solution might be to define a function to increase the number of escapes of all quotes in a string and use that to format all our quoted arguments.

Testing Details 🔍

Added another unit test to cURLRepresentation to cover this case.

@jshier jshier merged commit 6359d95 into Alamofire:master Mar 31, 2018

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed

This comment has been minimized.


jshier commented Mar 31, 2018

Thanks for the PR! Looks good to me! This should go out with 4.7.1.

@jshier jshier modified the milestones: Next, 4.7.1 Mar 31, 2018


This comment has been minimized.


jonah-williams commented Apr 3, 2018

@jshier awesome, thanks so much for the quick merge.

staneleigh added a commit to staneleigh/Alamofire that referenced this pull request Apr 5, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment