fix: 接口导入添加body请求头

This commit is contained in:
chenjianxing 2021-01-25 16:39:48 +08:00
parent e5ce5b740f
commit e11b1314bf
4 changed files with 32 additions and 2 deletions

View File

@ -125,6 +125,33 @@ public abstract class ApiImportAbstractParser implements ApiImportParser {
return bodyType;
}
protected void addBodyHeader(MsHTTPSamplerProxy request) {
String contentType = "";
if (request.getBody() != null && StringUtils.isNotBlank(request.getBody().getType())) {
switch (request.getBody().getType()) {
case Body.WWW_FROM:
contentType = "application/x-www-form-urlencoded";
break;
case Body.JSON:
contentType = "application/json";
break;
case Body.XML:
contentType = "application/xml";
break;
case Body.BINARY:
contentType = "application/octet-stream";
break;
}
List<KeyValue> headers = request.getHeaders();
if (headers == null) {
headers = new ArrayList<>();
request.setHeaders(headers);
}
addContentType(request.getHeaders(), contentType);
}
}
protected ApiDefinitionResult buildApiDefinition(String id, String name, String path, String method) {
ApiDefinitionResult apiDefinition = new ApiDefinitionResult();
apiDefinition.setName(name);
@ -168,8 +195,8 @@ public abstract class ApiImportAbstractParser implements ApiImportParser {
return request;
}
protected void addContentType(HttpRequest request, String contentType) {
// addHeader(request, "Content-Type", contentType);
protected void addContentType(List<KeyValue> headers, String contentType) {
addHeader(headers, "Content-Type", contentType);
}
protected void addCookie(List<KeyValue> headers, String key, String value) {

View File

@ -74,6 +74,7 @@ public class PostmanParser extends ApiImportAbstractParser {
parseBody(request.getBody(), requestDesc);
request.setArguments(parseKeyValue(url.getQuery()));
request.setHeaders(parseKeyValue(requestDesc.getHeader()));
addBodyHeader(request);
apiDefinition.setRequest(JSON.toJSONString(request));
return apiDefinition;
}

View File

@ -68,6 +68,7 @@ public class Swagger2Parser extends SwaggerAbstractParser {
MsHTTPSamplerProxy request = buildRequest(operation, pathName, method.name());
ApiDefinitionResult apiDefinition = buildApiDefinition(request.getId(), operation, pathName, method.name());
parseParameters(operation, request);
addBodyHeader(request);
apiDefinition.setRequest(JSON.toJSONString(request));
apiDefinition.setResponse(JSON.toJSONString(parseResponse(operation, operation.getResponses())));
buildModule(parentNode, apiDefinition, operation.getTags(), importRequest.isSaved());

View File

@ -100,6 +100,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
ApiDefinitionResult apiDefinition = buildApiDefinition(request.getId(), operation, pathName, method);
parseParameters(operation, request);
parseRequestBody(operation.getRequestBody(), request.getBody());
addBodyHeader(request);
apiDefinition.setRequest(JSON.toJSONString(request));
apiDefinition.setResponse(JSON.toJSONString(parseResponse(operation.getResponses())));
buildModule(parentNode, apiDefinition, operation.getTags(), importRequest.isSaved());