I have a curl php
request as shown below
$curlInit = curl_init(); curl_setopt($curlInit, CURLOPT_URL, 'https://www.myurl.com/'); curl_setopt($curlInit, CURLOPT_POST, 1); curl_setopt($curlInit, CURLOPT_POSTFIELDS, http_build_query( array( 'action' => 'GetSearchDetails', 'username' => 'lambistic', 'password' => 'lambistic######', 'responsetype' => 'json', ) ) ); curl_setopt($curlInit, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($curlInit); curl_close($curlInit);
What I want to achieve is to perform the same operation in springboot java using web client
, below is how I try to do it using web client
public Mono<SearchDetailsResponse> sendSearchDetailsRequest() { return webClient.post() .uri("https://www.myurl.com/") .header("Content-Type", "application/json") .headers(httpHeaders -> { httpHeaders.set("username", "lambistic"); httpHeaders.set("password", "lambistic######"); }) .retrieve() .bodyToMono(SearchDetailsResponse.class); }
I'm not getting any reply, my web client may be doing something wrong
P粉5101277412024-02-18 15:02:36
http_build_query Add parameters as query string. In your web client you add them as headers. I think you have to change this:
webClient .post() .uri("https://www.myurl.com?action=GetSearchDetails&username=lambistic&password=lambistic#####&responsetype=json") .retrieve() .bodyToMono(SearchDetailsResponse.class);