Skip to content

Commit ed107d4

Browse files
committed
fix: 如果 requestType 是 json,自动增加header content-type/json
1 parent 5939869 commit ed107d4

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### Todo
2+
* 修复接口更新时,如果 requestType 是 json,自动增加header "content-type/json"
3+
14
### v1.3.23
25

36
* 接口tag功能

server/controllers/interface.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,49 @@ const path = require('path');
1919
// const annotatedCss = require("jsondiffpatch/public/formatters-styles/annotated.css");
2020
// const htmlCss = require("jsondiffpatch/public/formatters-styles/html.css");
2121

22+
23+
function handleHeaders(values){
24+
let isfile = false,
25+
isHavaContentType = false;
26+
if (values.req_body_type === 'form') {
27+
values.req_body_form.forEach(item => {
28+
if (item.type === 'file') {
29+
isfile = true;
30+
}
31+
});
32+
33+
values.req_headers.map(item => {
34+
if (item.name === 'Content-Type') {
35+
item.value = isfile ? 'multipart/form-data' : 'application/x-www-form-urlencoded';
36+
isHavaContentType = true;
37+
}
38+
});
39+
if (isHavaContentType === false) {
40+
values.req_headers.unshift({
41+
name: 'Content-Type',
42+
value: isfile ? 'multipart/form-data' : 'application/x-www-form-urlencoded'
43+
});
44+
}
45+
} else if (values.req_body_type === 'json') {
46+
values.req_headers
47+
? values.req_headers.map(item => {
48+
if (item.name === 'Content-Type') {
49+
item.value = 'application/json';
50+
isHavaContentType = true;
51+
}
52+
})
53+
: [];
54+
if (isHavaContentType === false) {
55+
values.req_headers = values.req_headers || [];
56+
values.req_headers.unshift({
57+
name: 'Content-Type',
58+
value: 'application/json'
59+
});
60+
}
61+
}
62+
}
63+
64+
2265
class interfaceController extends baseController {
2366
constructor(ctx) {
2467
super(ctx);
@@ -183,6 +226,8 @@ class interfaceController extends baseController {
183226
));
184227
}
185228

229+
handleHeaders(params)
230+
186231
params.query_path = {};
187232
params.query_path.path = http_path.pathname;
188233
params.query_path.params = [];
@@ -550,6 +595,8 @@ class interfaceController extends baseController {
550595
// params.res_body_is_json_schema = _.isUndefined (params.res_body_is_json_schema) ? true : params.res_body_is_json_schema;
551596
// params.req_body_is_json_schema = _.isUndefined(params.req_body_is_json_schema) ? true : params.req_body_is_json_schema;
552597

598+
handleHeaders(params)
599+
553600
let interfaceData = await this.Model.get(id);
554601
if (!interfaceData) {
555602
return (ctx.body = yapi.commons.resReturn(null, 400, '不存在的接口'));

0 commit comments

Comments
 (0)