-
Notifications
You must be signed in to change notification settings - Fork 2
/
formServer.js
27 lines (25 loc) · 1.03 KB
/
formServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var middleware = require('./middleware')
var isStream = require('../isStream')
var setBodyToString = require('../setBodyToString')
var setHeaderTo = require('../setHeaderTo')
var shouldParseAs = require('../shouldParseAs')
var readBodyAsString = require('../readBodyAsString')
var querystringLite = require('../querystring-lite')
module.exports = middleware('form', function (request, next) {
if (request.options.form && request.body instanceof Object && !isStream(request.body)) {
var querystring = request.options.qs || querystringLite
setBodyToString(request, querystring.stringify(request.body))
setHeaderTo(request, 'content-type', 'application/x-www-form-urlencoded')
}
return next().then(function (response) {
if (shouldParseAs(response, 'form', request)) {
return readBodyAsString(response).then(function () {
var querystring = request.options.qs || querystringLite
response.body = querystring.parse(response.stringBody)
return response
})
} else {
return response
}
})
})