anyproxy web端无法显示Content-Type为application/json的body

bxgwgixi  于 2023-03-19  发布在  其他
关注(0)|答案(3)|浏览(246)

步骤:

  1. 连接代理,抓取请求,如果response header中的Content-Type是application/json;charset=utf-8,web端展示response body正常
  2. 添加规则,对某一个请求处理response body后,response header中的Content-Type是application/json;charset=utf-8,web端展示response body不正常,展示的是request query param
  3. 规则中修改Content-Type为json,web端对response body展示正常,但是连接代理的客户端会不识别
9q78igpj

9q78igpj1#

@WonderfulLing
可否提供这个规则的处理逻辑,是如何处理resonse body的?

gtlvzcf8

gtlvzcf82#

beforeSendResponse
var newRequestOptions = requestDetail.requestOptions;
// const urlPattern = url.parse(requestDetail.url);
// const pathname = urlPattern.pathname;
// var content_type = responseDetail.response.header['Content-Type'];
// if (content_type != 'undefined' && content_type != '' && content_type != null) {
// responseDetail.response.header['Content-Type'] = 'json';
// }
var url_info = requestDetail.url;
var newResponse = responseDetail.response;
if (global.userRule != '' && global.userRule != 'undefined' && global.userRule != null && global.userRule.length > 0) {
for (var inx in global.userRule) {
var data = global.userRule[inx];
var reg = data['url'];
if (url_info.indexOf(reg) > -1) {
if (data['res_header'] && data['type'] === 'rewrite') {
for (var key in JSON.parse(data['res_header'])) {
newResponse['header'][key] = JSON.parse(data['res_header'])[key];
}
}
return {
response: newResponse
};
}
}
}

xt0899hw

xt0899hw3#

beforeSendRequest
if (url_info.indexOf(reg) > -1) {
newRequestOptions.headers['type'] = data['type'];
switch (data['type']) {
case 'map local':
local_success_resp = {
statusCode: 200,
header: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"message": "success"
})
};
local_success_resp['body'] = global.userRule[inx]['res_body'];
local_success_resp['header']['TMOCK'] = "success";
return {
response: local_success_resp
};
break;

相关问题