fix: 拼接请求参数、请求header
This commit is contained in:
parent
97ecfbd698
commit
387577cd02
|
@ -67,12 +67,26 @@ export default {
|
||||||
}
|
}
|
||||||
let code = "";
|
let code = "";
|
||||||
if (data.length > 0) {
|
if (data.length > 0) {
|
||||||
data.forEach(dt => {
|
for (let dt of data) {
|
||||||
|
// 过滤非HTTP接口API
|
||||||
|
if (dt.protocol !== "HTTP") {
|
||||||
|
if (!dt.request) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
// 是否是HTTP接口CASE
|
||||||
|
if (dt.request) {
|
||||||
|
let req = JSON.parse(dt.request);
|
||||||
|
if (req.protocol !== "HTTP") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
let param = this._parseRequestObj(dt);
|
let param = this._parseRequestObj(dt);
|
||||||
param['host'] = host;
|
param['host'] = host;
|
||||||
param['protocol'] = protocol;
|
param['protocol'] = protocol;
|
||||||
code += '\n' + getCodeTemplate(this.language, param);
|
code += '\n' + getCodeTemplate(this.language, param);
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
this.handleCodeTemplate(code);
|
this.handleCodeTemplate(code);
|
||||||
this.$refs.apiFuncRelevance.close();
|
this.$refs.apiFuncRelevance.close();
|
||||||
|
@ -82,6 +96,7 @@ export default {
|
||||||
},
|
},
|
||||||
_parseRequestObj(data) {
|
_parseRequestObj(data) {
|
||||||
let requestHeaders = new Map();
|
let requestHeaders = new Map();
|
||||||
|
let requestArguments = new Map();
|
||||||
let requestMethod = "";
|
let requestMethod = "";
|
||||||
let requestBody = "";
|
let requestBody = "";
|
||||||
let requestPath = "";
|
let requestPath = "";
|
||||||
|
@ -97,11 +112,19 @@ export default {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
let args = request.arguments;
|
||||||
|
if (args && args.length) {
|
||||||
|
args.forEach(arg => {
|
||||||
|
if (arg.name) {
|
||||||
|
requestArguments.set(arg.name, arg.value);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
let body = request.body;
|
let body = request.body;
|
||||||
if (body.json) {
|
if (body.json) {
|
||||||
requestBody = body.raw;
|
requestBody = body.raw;
|
||||||
}
|
}
|
||||||
return {requestPath, requestHeaders, requestMethod, requestBody}
|
return {requestPath, requestHeaders, requestMethod, requestBody, requestArguments}
|
||||||
},
|
},
|
||||||
apiClose() {
|
apiClose() {
|
||||||
|
|
||||||
|
|
|
@ -24,17 +24,15 @@ export function getCodeTemplate(language, requestObj) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function groovyCode(requestObj) {
|
function groovyCode(requestObj) {
|
||||||
let {requestHeaders = new Map(), requestBody = "", requestPath = "", requestMethod = "", host = "", protocol = ""} = requestObj;
|
let {requestHeaders = new Map(), requestBody = "", requestPath = "",
|
||||||
|
requestMethod = "", host = "", protocol = "", requestArguments = new Map()} = requestObj;
|
||||||
let requestUrl = "";
|
let requestUrl = "";
|
||||||
|
requestPath = getRequestPath(requestArguments, requestPath);
|
||||||
if (protocol && host && requestPath) {
|
if (protocol && host && requestPath) {
|
||||||
requestUrl = protocol + "://" + host + requestPath;
|
requestUrl = protocol + "://" + host + requestPath;
|
||||||
}
|
}
|
||||||
let headers = "", body = JSON.stringify(requestBody);
|
let body = JSON.stringify(requestBody);
|
||||||
for (let [k, v] of requestHeaders) {
|
let headers = getHeaders(requestHeaders);
|
||||||
// 拼装映射
|
|
||||||
headers += `['${k}':'${v}']`;
|
|
||||||
}
|
|
||||||
headers = headers === "" ? "[]" : headers;
|
|
||||||
let params = "";
|
let params = "";
|
||||||
params += `[
|
params += `[
|
||||||
'url': '${requestUrl}',
|
'url': '${requestUrl}',
|
||||||
|
@ -45,9 +43,7 @@ function groovyCode(requestObj) {
|
||||||
return `import groovy.json.JsonOutput
|
return `import groovy.json.JsonOutput
|
||||||
import groovy.json.JsonSlurper
|
import groovy.json.JsonSlurper
|
||||||
|
|
||||||
def http_post() {
|
|
||||||
def params = ${params}
|
def params = ${params}
|
||||||
|
|
||||||
def headers = params['headers']
|
def headers = params['headers']
|
||||||
// json数据
|
// json数据
|
||||||
def data = params['data']
|
def data = params['data']
|
||||||
|
@ -66,24 +62,19 @@ def http_post() {
|
||||||
writer.close()
|
writer.close()
|
||||||
}
|
}
|
||||||
log.info(conn.content.text)
|
log.info(conn.content.text)
|
||||||
}
|
|
||||||
http_post()
|
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function pythonCode(requestObj) {
|
function pythonCode(requestObj) {
|
||||||
let {requestHeaders = new Map(), requestBody = "", requestPath = "", requestMethod = "", host = "", protocol = "http"} = requestObj;
|
let {requestHeaders = new Map(), requestBody = "", requestPath = "/",
|
||||||
|
requestMethod = "", host = "", protocol = "http", requestArguments = new Map()} = requestObj;
|
||||||
let connType = "HTTPConnection";
|
let connType = "HTTPConnection";
|
||||||
if (protocol === 'https') {
|
if (protocol === 'https') {
|
||||||
connType = "HTTPSConnection";
|
connType = "HTTPSConnection";
|
||||||
}
|
}
|
||||||
let headers = "";
|
let headers = getHeaders(requestHeaders);
|
||||||
for (let [k, v] of requestHeaders) {
|
|
||||||
// 拼装
|
|
||||||
headers += `{'${k}':'${v}'}`;
|
|
||||||
}
|
|
||||||
headers = headers === "" ? "{}" : headers;
|
|
||||||
requestBody = requestBody ? requestBody : "{}";
|
requestBody = requestBody ? requestBody : "{}";
|
||||||
|
requestPath = getRequestPath(requestArguments, requestPath);
|
||||||
return `import httplib
|
return `import httplib
|
||||||
params = ${requestBody} #例 {'username':'test'}
|
params = ${requestBody} #例 {'username':'test'}
|
||||||
headers = ${headers} #例 {'Content-Type':'application/json'}
|
headers = ${headers} #例 {'Content-Type':'application/json'}
|
||||||
|
@ -96,7 +87,7 @@ conn.request(method, path, params, headers)
|
||||||
res = conn.getresponse()
|
res = conn.getresponse()
|
||||||
data = unicode(res.read(), 'utf-8')
|
data = unicode(res.read(), 'utf-8')
|
||||||
log.info(data)
|
log.info(data)
|
||||||
`
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function javaCode(requestObj) {
|
function javaCode(requestObj) {
|
||||||
|
@ -106,3 +97,33 @@ function javaCode(requestObj) {
|
||||||
function jsCode(requestObj) {
|
function jsCode(requestObj) {
|
||||||
return ``;
|
return ``;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getRequestPath(requestArgs, requestPath) {
|
||||||
|
if (requestArgs.size > 0) {
|
||||||
|
requestPath = requestPath + "?"
|
||||||
|
let index = 1;
|
||||||
|
for (let [k, v] of requestArgs) {
|
||||||
|
if (index !== 1) {
|
||||||
|
requestPath = requestPath + "&";
|
||||||
|
}
|
||||||
|
requestPath = requestPath + k + "=" + v;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return requestPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getHeaders(requestHeaders) {
|
||||||
|
let headers = "{";
|
||||||
|
let index = 1;
|
||||||
|
for (let [k, v] of requestHeaders) {
|
||||||
|
if (index !== 1) {
|
||||||
|
headers += ",";
|
||||||
|
}
|
||||||
|
// 拼装
|
||||||
|
headers += `'${k}':'${v}'`;
|
||||||
|
index ++;
|
||||||
|
}
|
||||||
|
headers = headers + "}"
|
||||||
|
return headers;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue