-
Notifications
You must be signed in to change notification settings - Fork 2k
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
cURL: Support all --data flags and fix parsing of key=value pairs #5818
cURL: Support all --data flags and fix parsing of key=value pairs #5818
Conversation
Updates tests to support key=value pairs in urlencode Issue: Kong#5696
Splits out the logic to handle the cases for the --data-urlencode flag from cURL. docs: https://curl.se/docs/manpage.html#--data-urlencode Issue: Kong#5696
This refactors the current implementation for handling --data[suffix] flags from cURL supporting all possible formats outside of reading files into Insomnia. docs: https://curl.se/docs/manpage.html Issues: Kong#5696, Kong#5400
Combines a lot of tests and covers all cases provided in the cURL man pages.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comments for implementation details and specific notes 😄
const header = `-H 'Content-Type: ${mimeType}'`; | ||
|
||
describe('encoding', () => { | ||
describe('cURL --data flags', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revamped the tests adding explicit checking for different command flags and arguments.
-d
, --data
, --data-raw
, --data-ascii
, --data-binary
, and --data-urlencode
/** | ||
* cURL supported -d, and --date[suffix] flags. | ||
*/ | ||
const dataFlags = [ | ||
/** | ||
* https://curl.se/docs/manpage.html#-d | ||
*/ | ||
'd', | ||
'data', | ||
|
||
/** | ||
* https://curl.se/docs/manpage.html#--data-raw | ||
*/ | ||
'data-raw', | ||
|
||
/** | ||
* https://curl.se/docs/manpage.html#--data-urlencode | ||
*/ | ||
'data-urlencode', | ||
|
||
/** | ||
* https://curl.se/docs/manpage.html#--data-binary | ||
*/ | ||
'data-binary', | ||
|
||
/** | ||
* https://curl.se/docs/manpage.html#--data-ascii | ||
*/ | ||
'data-ascii', | ||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Document with links to their sections in the man page
* | ||
* @param keyedPairs pairs with cURL flags as keys. | ||
*/ | ||
const pairsToDataParameters = (keyedPairs: PairsByName): Parameter[] => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracted out textBodyParams
section, Parameter[]
are just generated in this method.
if (pair.includes('@') && allowFiles) { | ||
const [name, fileName] = pair.split('@'); | ||
return { name, fileName, type: 'file' }; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following the man pages from cURL, it's only ever documented in --data-urlencode
that you can specify files via
name@filename
syntax, this has the consequence of all --data
flags supporting that format for code simplicity.
case 'd': | ||
case 'data': | ||
case 'data-ascii': | ||
case 'data-binary': | ||
dataParameters = dataParameters.concat(pairs.flatMap(pair => pairToParameters(pair, true))); | ||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these behave the same in this context, as they all support files (technically in different ways)
return pair.split('&').map(pair => { | ||
if (pair.includes('@') && allowFiles) { | ||
const [name, fileName] = pair.split('@'); | ||
return { name, fileName, type: 'file' }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously the type
file was never specified for --data
flags.
case 'data-raw': | ||
dataParameters = dataParameters.concat(pairs.flatMap(pair => pairToParameters(pair))); | ||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data-raw
is just data
, but doesn't parse the @
specially. So it just doesn't support files at all.
default: | ||
throw new Error(`unhandled data flag ${flagName}`); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should never happen unless a new flag was added to dataFlags
, but never handled here.
…insomnia into curl-parse-urlencode-params
Hello @filfreire (tagging you because you've been active on tagged issues), this PR is open to review whenever is convenient for you and your team 🚀 Please let me know if there's commits I need to rebase or required changes (also if I missed anything in the contributing guidelines). Happy to make any changes here, just trying to help resolve some open issues and give back to the project 😃 |
Thanks for contributing @NickHackman, we'll look into this asap. cc @Kong/team-insomnia |
"Test/OS (windows-latest)" failed due to a connection error
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow, awesome work thanks @NickHackman
@NickHackman you can claim a free tshirt for this PR contribution - find more at https://konghq.com/community/open-source-contribution |
changelog(Improvements): Improved parsing for cURL imports, supporting different data types.
Closes #5696
Closes #5400
Changes
key=value
in--data-urlencode
data
types and handling their specific casesImplementation follows https://curl.se/docs/manpage.html