api client body 처리 수정
This commit is contained in:
@@ -10,7 +10,7 @@ public class ApiRequestDTO {
|
||||
private Long server;
|
||||
|
||||
private List<ApiHeaderDTO> headers;
|
||||
private String body;
|
||||
private String requestBody;
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
@@ -44,11 +44,11 @@ public class ApiRequestDTO {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return body;
|
||||
public String getRequestBody() {
|
||||
return requestBody;
|
||||
}
|
||||
|
||||
public void setBody(String body) {
|
||||
this.body = body;
|
||||
public void setRequestBody(String requestBody) {
|
||||
this.requestBody = requestBody;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.eactive.httpmockserver.client.dto.ApiResponse;
|
||||
import com.eactive.httpmockserver.client.entity.Server;
|
||||
import com.eactive.httpmockserver.client.repository.ServerRepository;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
@@ -16,6 +17,8 @@ import io.netty.handler.codec.http.*;
|
||||
import io.netty.handler.ssl.SslContext;
|
||||
import io.netty.handler.ssl.SslContextBuilder;
|
||||
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -44,7 +47,7 @@ public class NettyApiClient {
|
||||
.handler(new ChannelInitializer<Channel>() {
|
||||
@Override
|
||||
public void initChannel(Channel ch) {
|
||||
if (server.getScheme().equalsIgnoreCase("https")){
|
||||
if (server.getScheme().equalsIgnoreCase("https")) {
|
||||
try {
|
||||
final SslContext sslCtx = SslContextBuilder.forClient()
|
||||
.trustManager(InsecureTrustManagerFactory.INSTANCE).build();
|
||||
@@ -62,13 +65,31 @@ public class NettyApiClient {
|
||||
});
|
||||
|
||||
Channel ch = b.connect(host, port).sync().channel();
|
||||
ByteBuf content = null;
|
||||
if (StringUtils.isNotEmpty(apiRequest.getRequestBody())) {
|
||||
content = Unpooled.copiedBuffer(apiRequest.getRequestBody(), CharsetUtil.UTF_8);
|
||||
} else {
|
||||
content = Unpooled.EMPTY_BUFFER;
|
||||
}
|
||||
|
||||
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.valueOf(apiRequest.getMethod()),
|
||||
apiRequest.getPath(), Unpooled.EMPTY_BUFFER);
|
||||
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.valueOf(apiRequest.getMethod()),
|
||||
apiRequest.getPath(), content);
|
||||
|
||||
request.headers().set("Host", host);
|
||||
request.headers().set("Connection", "close");
|
||||
request.headers().set("Accept-Encoding", "gzip");
|
||||
request.headers().set("Connection", HttpHeaderValues.CLOSE);
|
||||
request.headers().set("Accept-Encoding", HttpHeaderValues.GZIP);
|
||||
|
||||
if (StringUtils.isNotEmpty(apiRequest.getRequestBody())) {
|
||||
request.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
|
||||
}
|
||||
|
||||
if (apiRequest.getHeaders().size() > 0) {
|
||||
HttpHeaders customHeaders = new DefaultHttpHeaders();
|
||||
apiRequest.getHeaders().forEach((header) -> {
|
||||
customHeaders.set(header.getKey(), header.getValue());
|
||||
});
|
||||
request.headers().add(customHeaders);
|
||||
}
|
||||
|
||||
ch.writeAndFlush(request);
|
||||
ch.closeFuture().sync();
|
||||
@@ -78,4 +99,6 @@ public class NettyApiClient {
|
||||
|
||||
return responseFuture;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user