如何使用 webclient post body x-www-form-urlencoded?
how to post body x-www-form-urlencoded using webclient?
MultiValueMap<String, String> body_data = new LinkedMultiValueMap();
body_data.add("param1", {param1});
...
WebClient webClient = WebClient.builder().baseUrl(api_url+request_url)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
.build();
String result = webClient.post().contentType(MediaType.APPLICATION_FORM_URLENCODED)
.bodyValue(BodyInserters.fromFormData(body_data)).retrieve().bodyToMono(String.class).block();
它returns
org.springframework.web.reactive.function.client.WebClientRequestException: Content type 'application/x-www-form-urlencoded' not supported for bodyType=org.springframework.web.reactive.function.BodyInserters$DefaultFormInserter; nested exception is org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/x-www-form-urlencoded' not supported for bodyType=org.springframework.web.reactive.function.BodyInserters$DefaultFormInserter
对此有什么建议吗?
内容类型应该是 application/x-www-form-urlencoded.
我们可以使用 BodyInserters.fromFormData 来达到这个目的:
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();
另一种形式:
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
formData.add("username", "XXXX");
formData.add("password", "XXXX");
String response = WebClient.create()
.post()
.uri("URL")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.body(BodyInserters.fromFormData(formData))
.exchange()
.block()
.bodyToMono(String.class)
.block();
MultiValueMap<String, String> body_data = new LinkedMultiValueMap();
body_data.add("param1", {param1});
...
WebClient webClient = WebClient.builder().baseUrl(api_url+request_url)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
.build();
String result = webClient.post().contentType(MediaType.APPLICATION_FORM_URLENCODED)
.bodyValue(BodyInserters.fromFormData(body_data)).retrieve().bodyToMono(String.class).block();
它returns
org.springframework.web.reactive.function.client.WebClientRequestException: Content type 'application/x-www-form-urlencoded' not supported for bodyType=org.springframework.web.reactive.function.BodyInserters$DefaultFormInserter; nested exception is org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/x-www-form-urlencoded' not supported for bodyType=org.springframework.web.reactive.function.BodyInserters$DefaultFormInserter
对此有什么建议吗? 内容类型应该是 application/x-www-form-urlencoded.
我们可以使用 BodyInserters.fromFormData 来达到这个目的:
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();
另一种形式:
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
formData.add("username", "XXXX");
formData.add("password", "XXXX");
String response = WebClient.create()
.post()
.uri("URL")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.body(BodyInserters.fromFormData(formData))
.exchange()
.block()
.bodyToMono(String.class)
.block();