Is it possible to send a form-url-encoded request with json in payload without actually encoding json ? Payload is of form jData=json.
I have tried various combination of form headers and BodyInserters, but it is not working, some time content header is wrong, other times body itself is totally JSON which again at the server API level is not desirable.
I have tried to overwrite request content in onRequestContent method in comment piece of code, hoping with this interception I would be able to override request, but still body is not changed.
Please help.
public class FinvasiaAuthenticationProvider implements BrokerAuthenticationProvider {
private static Logger LOGGER = LoggerFactory.getLogger(FinvasiaAuthenticationProvider.class);
private final WebClient client;
private final FinvasiaProperties properties;
private final ObjectMapper mapper;
public FinvasiaAuthenticationProvider(FinvasiaProperties properties,
ObjectMapper mapper) {
this.client = this.jettyHttpClient();
this.properties = properties;
this.mapper = mapper;
}
@Override
public Mono<BrokerAuthentication> authenticate(BrokerAuthenticationRequest req) {
if (!(req instanceof FinvasiaAuthenticationRequest)) {
return Mono.error(IllegalArgumentException::new);
}
var endpoint = String.format("%s/%s", properties.baseUrl(), FinvasiaUrls.LOGIN_URL.url());
var payload = new FinvasiaAuthenticationRequestAdapter(((FinvasiaAuthenticationRequest) req));
String json;
try {
json = mapper.writeValueAsString(payload);
} catch (JsonProcessingException e) {
return Mono.error(e);
}
var hello = "Hello";
Map<String, String> map = new HashMap<>();
map.put("jData", json);
return client.post()
.uri(endpoint)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
.body(BodyInserters.fromFormData("jData", json))
.retrieve()
.onStatus(HttpStatus::is4xxClientError, clientResponse -> {
clientResponse.bodyToMono(String.class).log().subscribe();
return Mono.error(IllegalArgumentException::new);
})
.bodyToMono(String.class)
.map(response -> {
return new FinvasiaAuthentication("1234", Arrays.asList());
});
}
private Request enhance(Request inboundRequest) {
StringBuilder log = new StringBuilder();
inboundRequest.onRequestBegin(request -> log.append("Request: \n")
.append("URI: ")
.append(request.getURI())
.append("\n")
.append("Method: ")
.append(request.getMethod()));
inboundRequest.onRequestHeaders(request -> {
log.append("\nRequest Headers:\n");
for (HttpField header : request.getHeaders()) {
log.append("\n" + header.getName() + ":" + header.getValue());
}
log.append("\n\n");
});
// inboundRequest.onRequestContent((request, content) -> {
//
//
// String b = StandardCharsets.UTF_8.decode(content).toString();
// String[] parts = StringUtils.split(b, '=');
// String decoded = UriUtils.decode(parts[1], StandardCharsets.UTF_8);
//
// content.clear();
// content.put(String.format("%s=%s", parts[0],decoded ).getBytes(StandardCharsets.UTF_8));
//
// request.content(n)
//
// });
inboundRequest.onRequestContent((request, content) ->
log.append("Body: \n\t")
.append(StandardCharsets.UTF_8.decode(content)));
log.append("\n");
inboundRequest.onResponseBegin(response -> {
log.append("Response:\n")
.append("Status: ")
.append(response.getStatus())
.append("\n");
});
inboundRequest.onResponseHeaders(response -> {
log.append("\nResponse Headers:\n");
for (HttpField header : response.getHeaders()) {
log.append("\n" + header.getName() + ":" + header.getValue());
}
log.append("\n\n");
});
inboundRequest.onResponseContent((respones, content) -> {
var bufferAsString = StandardCharsets.UTF_8.decode(content).toString();
log.append("Response Body:\n" + bufferAsString);
});
LOGGER.info("HTTP -> \n");
inboundRequest.onRequestSuccess(request -> LOGGER.info(log.toString()));
inboundRequest.onResponseSuccess(response -> LOGGER.info(log.toString()));
inboundRequest.onResponseFailure((response, throwable) -> LOGGER.info(log.toString()));
return inboundRequest;
}
public WebClient jettyHttpClient() {
SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
HttpClient httpClient = new HttpClient(sslContextFactory) {
@Override
public Request newRequest(URI uri) {
Request request = super.newRequest(uri);
return enhance(request);
}
};
return WebClient.builder().clientConnector(new JettyClientHttpConnector(httpClient))
// .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
.build();
}
}