@@ -29,19 +29,31 @@ var http = require('http'),
29
29
request = require ( 'request' ) ,
30
30
colors = require ( 'colors' ) ,
31
31
util = require ( 'util' ) ,
32
+ queryString = require ( 'querystring' ) ,
32
33
bodyParser = require ( 'body-parser' ) ,
33
34
httpProxy = require ( '../../lib/http-proxy' ) ,
34
35
proxy = httpProxy . createProxyServer ( { } ) ;
35
36
36
37
37
38
//restream parsed body before proxying
38
39
proxy . on ( 'proxyReq' , function ( proxyReq , req , res , options ) {
39
- if ( req . body ) {
40
- let bodyData = JSON . stringify ( req . body ) ;
41
- // incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
42
- proxyReq . setHeader ( 'Content-Type' , 'application/json' ) ;
40
+ if ( ! req . body || ! Object . keys ( req . body ) . length ) {
41
+ return ;
42
+ }
43
+
44
+ var contentType = proxyReq . getHeader ( 'Content-Type' ) ;
45
+ var bodyData ;
46
+
47
+ if ( contentType === 'application/json' ) {
48
+ bodyData = JSON . stringify ( req . body ) ;
49
+ }
50
+
51
+ if ( contentType === 'application/x-www-form-urlencoded' ) {
52
+ bodyData = queryString . stringify ( req . body ) ;
53
+ }
54
+
55
+ if ( bodyData ) {
43
56
proxyReq . setHeader ( 'Content-Length' , Buffer . byteLength ( bodyData ) ) ;
44
- // stream the content
45
57
proxyReq . write ( bodyData ) ;
46
58
}
47
59
} ) ;
@@ -94,5 +106,3 @@ http.createServer(app1).listen(9013, function(){
94
106
console . log ( 'return for urlencoded request:' , err , data )
95
107
} )
96
108
} ) ;
97
-
98
-
0 commit comments