Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post request with spring boot web-client for Form data for content type application/x-www-form-urlencoded

How To use spring boot webclient for posting request with content type application/x-www-form-urlencoded sample curl request with content type `application/x-www-form-urlencoded'

--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'username=XXXX' \
--data-urlencode 'password=XXXX'

How Can i send same request using webclient?

like image 299
Niraj Sonawane Avatar asked Jan 17 '20 17:01

Niraj Sonawane


People also ask

How do you send POST request with X www form Urlencoded body in Java HttpURLConnection?

String urlParameters = cafedra_name+ data_to_send; URL url; HttpURLConnection connection = null; try { //Create connection url = new URL(targetURL); connection = (HttpURLConnection)url. openConnection(); connection. setRequestMethod("POST"); connection.

Which annotations access application X www form Urlencoded parameters?

Now notice, we have used @ModelAttribute above to map the incoming application/x-www-form-urlencoded or multipart/form-data directly to the Student model class. The @ModelAttribute is an annotation that binds a method parameter or method return value to a named model attribute and then exposes it to a web view.


1 Answers

We can use BodyInserters.fromFormData for this purpose

webClient client = WebClient.builder()
        .baseUrl("SOME-BASE-URL")
        .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
        .build();

return client.post()
        .uri("SOME-URI)
        .body(BodyInserters.fromFormData("username", "SOME-USERNAME")
                .with("password", "SONE-PASSWORD"))
                .retrieve()
                .bodyToFlux(SomeClass.class)
                .onErrorMap(e -> new MyException("messahe",e))
        .blockLast();
    
like image 92
Niraj Sonawane Avatar answered Sep 17 '22 19:09

Niraj Sonawane