@@ -14,6 +14,16 @@ const util = require('util')
1414const CodeBuilder = require ( '../../helpers/code-builder' )
1515const helpers = require ( './helpers' )
1616
17+ const builtInMethods = [
18+ 'HEAD' ,
19+ 'GET' ,
20+ 'POST' ,
21+ 'PUT' ,
22+ 'PATCH' ,
23+ 'DELETE' ,
24+ 'OPTIONS'
25+ ]
26+
1727module . exports = function ( source , options ) {
1828 const opts = Object . assign ( {
1929 indent : ' ' ,
@@ -34,7 +44,7 @@ module.exports = function (source, options) {
3444 // Construct query string
3545 let qs
3646 if ( Object . keys ( source . queryObj ) . length ) {
37- qs = 'querystring = ' + JSON . stringify ( source . queryObj )
47+ qs = 'querystring = ' + helpers . literalRepresentation ( source . queryObj , opts )
3848
3949 code . push ( qs )
4050 . blank ( )
@@ -52,6 +62,14 @@ module.exports = function (source, options) {
5262 }
5363 break
5464
65+ case 'application/x-www-form-urlencoded' :
66+ if ( source . postData . paramsObj ) {
67+ code . push ( 'payload = %s' , helpers . literalRepresentation ( source . postData . paramsObj , opts ) )
68+ hasPayload = true
69+ break
70+ }
71+ // Otherwise, fall through to treat as plain text:
72+
5573 default : {
5674 const payload = JSON . stringify ( source . postData . text )
5775 if ( payload ) {
@@ -67,7 +85,7 @@ module.exports = function (source, options) {
6785
6886 if ( headerCount === 1 ) {
6987 for ( const header in headers ) {
70- code . push ( 'headers = {"%s": "%s"}' , header , headers [ header ] )
88+ code . push ( 'headers = { "%s": "%s" }' , header , headers [ header ] )
7189 . blank ( )
7290 }
7391 } else if ( headerCount > 1 ) {
@@ -89,7 +107,10 @@ module.exports = function (source, options) {
89107
90108 // Construct request
91109 const method = source . method
92- let request = util . format ( 'response = requests.request("%s", url' , method )
110+
111+ let request = builtInMethods . includes ( method )
112+ ? util . format ( 'response = requests.%s(url' , method . toLowerCase ( ) )
113+ : util . format ( 'response = requests.request("%s", url' , method )
93114
94115 if ( hasPayload ) {
95116 if ( jsonPayload ) {
0 commit comments