From e526f32b4a8710e30dac0967540c3cefb37dbd02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Thu, 17 Dec 2020 10:36:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96content-type=E7=9A=84?= =?UTF-8?q?=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 11 ++++++++--- package.json | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index fa585f5..db5f48e 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,8 @@ import URL from 'url' import QS from 'querystring' import PATH from 'path' +const DEFAULT_FORM_TYPE = 'application/x-www-form-urlencoded' + var __dirname = PATH.dirname(URL.fileURLToPath(import.meta.url)) var tmpdir = PATH.resolve(__dirname, './.tmp/') @@ -147,6 +149,7 @@ export default class Request { post(key = '', xss = true) { let para = {} let out = Promise.defer() + let form, contentType xss = !!xss //如果之前已经缓存过,则直接从缓存读取 @@ -158,7 +161,9 @@ export default class Request { } } - let form = new Parser() + contentType = this.header('content-type') || DEFAULT_FORM_TYPE + + form = new Parser() form.uploadDir = tmpdir form.parse(this.origin.req) @@ -167,7 +172,7 @@ export default class Request { para = value return } - if (~this.header('content-type').indexOf('urlencoded')) { + if (~contentType.indexOf('urlencoded')) { if ( name.slice(0, 2) === '{"' && (name.slice(-2) === '"}' || value.slice(-2) === '"}') @@ -237,7 +242,7 @@ export default class Request { form.on('error', out.reject) form.on('end', err => { - if (~this.header('content-type').indexOf('urlencoded')) { + if (~contentType.indexOf('urlencoded')) { for (let i in para) { if (typeof para[i] === 'string') { if (!para[i]) { diff --git a/package.json b/package.json index 46474f3..35f016a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gm5/request", - "version": "1.2.3", + "version": "1.2.4", "description": "对Http的request进一步封装, 提供常用的API", "main": "index.js", "author": "yutent",