@@ -130,33 +130,43 @@ self = module.exports = {
130130 case 'urlencoded' :
131131 _ . forEach ( body . urlencoded , function ( data ) {
132132 if ( ! data . disabled ) {
133- // Using the long form below without considering the longFormat option,
134- // to generate more accurate and correct snippet
135- snippet += indent + '--data-urlencode' ;
136- snippet += ` ${ quoteType } ${ sanitize ( data . key , trim , quoteType ) } =` +
137- `${ sanitize ( data . value , trim , quoteType ) } ${ quoteType } ` ;
133+ snippet += indent + ( format ? '--data-urlencode' : '-d' ) ;
134+ snippet += ` ${ quoteType } ${ sanitize ( data . key , trim , quoteType , false , true ) } =` +
135+ `${ sanitize ( data . value , trim , quoteType , false , ! format ) } ${ quoteType } ` ;
138136 }
139137 } ) ;
140138 break ;
141- case 'raw' :
142- snippet += indent + `--data-raw ${ quoteType } ${ sanitize ( body . raw . toString ( ) , trim , quoteType ) } ${ quoteType } ` ;
139+ case 'raw' : {
140+ let rawBody = body . raw . toString ( ) ,
141+ isAsperandPresent = _ . includes ( rawBody , '@' ) ,
142+ // Use the long option if `@` is present in the request body otherwise follow user setting
143+ optionName = isAsperandPresent ? '--data-raw' : form ( '-d' , format ) ;
144+ snippet += indent + `${ optionName } ${ quoteType } ${ sanitize ( rawBody , trim , quoteType ) } ${ quoteType } ` ;
143145 break ;
146+ }
144147
145- case 'graphql' :
148+ case 'graphql' : {
146149 // eslint-disable-next-line no-case-declarations
147150 let query = body . graphql ? body . graphql . query : '' ,
148- graphqlVariables ;
151+ graphqlVariables , requestBody , isAsperandPresent , optionName ;
149152 try {
150153 graphqlVariables = JSON . parse ( body . graphql . variables ) ;
151154 }
152155 catch ( e ) {
153156 graphqlVariables = { } ;
154157 }
155- snippet += indent + `--data-raw ${ quoteType } ${ sanitize ( JSON . stringify ( {
158+
159+ requestBody = JSON . stringify ( {
156160 query : query ,
157161 variables : graphqlVariables
158- } ) , trim , quoteType ) } ${ quoteType } `;
162+ } ) ;
163+
164+ isAsperandPresent = _ . includes ( requestBody , '@' ) ;
165+ // Use the long option if `@` is present in the request body otherwise follow user setting
166+ optionName = isAsperandPresent ? '--data-raw' : form ( '-d' , format ) ;
167+ snippet += indent + `${ optionName } ${ quoteType } ${ sanitize ( requestBody , trim , quoteType ) } ${ quoteType } ` ;
159168 break ;
169+ }
160170 case 'formdata' :
161171 _ . forEach ( body . formdata , function ( data ) {
162172 if ( ! ( data . disabled ) ) {
@@ -179,7 +189,7 @@ self = module.exports = {
179189 } ) ;
180190 break ;
181191 case 'file' :
182- snippet += indent + '--data-binary' ;
192+ snippet += indent + form ( '-d' , format ) ;
183193 snippet += ` ${ quoteType } @${ sanitize ( body [ body . mode ] . src , trim ) } ${ quoteType } ` ;
184194 break ;
185195 default :
0 commit comments