fix: 拼接请求参数、请求header

This commit is contained in:
shiziyuan9527 2021-09-18 15:45:33 +08:00 committed by 刘瑞斌
parent 97ecfbd698
commit 387577cd02
2 changed files with 84 additions and 40 deletions

View File

@ -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) {
// HTTPAPI
if (dt.protocol !== "HTTP") {
if (!dt.request) {
continue;
} else {
// HTTPCASE
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() {

View File

@ -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,45 +43,38 @@ 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']
// json数据
def headers = params['headers'] def data = params['data']
// json数据 def conn = new URL(params['url']).openConnection()
def data = params['data'] conn.setRequestMethod(params['method'])
def conn = new URL(params['url']).openConnection() if (data) {
conn.setRequestMethod(params['method']) headers.each {
if (data) { k,v -> conn.setRequestProperty(k, v);
headers.each { }
k,v -> conn.setRequestProperty(k, v); // 输出请求参数
} log.info(data)
// 输出请求参数 conn.doOutput = true
log.info(data) def writer = new OutputStreamWriter(conn.outputStream)
conn.doOutput = true writer.write(data)
def writer = new OutputStreamWriter(conn.outputStream) writer.flush()
writer.write(data) writer.close()
writer.flush()
writer.close()
}
log.info(conn.content.text)
} }
http_post() log.info(conn.content.text)
`; `;
} }
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;
}