blob: 3b7314bab91de06be3f872625bbb2f2d65a70ca6 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001var TypedError = require("error/typed")
2
3var parseArguments = require("./parse-arguments.js")
4var jsonBody = require("./json.js")
5var formBody = require("./form.js")
6
7var jsonType = "application/json"
8var formType = "application/x-www-form-urlencoded"
9var INVALID_CONTENT_TYPE = TypedError({
10 message: "Could not parse content type header: {contentType}",
11 type: "invalid.content.type",
12 statusCode: 415,
13 contentType: null
14})
15
16module.exports = anyBody
17
18function anyBody(req, res, opts, callback) {
19 var args = parseArguments(req, res, opts, callback)
20 req = args.req
21 res = args.res
22 opts = args.opts
23 callback = args.callback
24
25 if (!callback) {
26 return anyBody.bind(null, req, res, opts)
27 }
28
29 var contentType = req.headers["content-type"] || ""
30
31 if (contentType.indexOf(jsonType) !== -1) {
32 jsonBody(req, res, opts, callback)
33 } else if (contentType.indexOf(formType) !== -1) {
34 formBody(req, res, opts, callback)
35 } else {
36 callback(INVALID_CONTENT_TYPE({contentType: contentType}))
37 }
38}