简体   繁体   English

如何使用 webclient 发布正文 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();

and it 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

Any suggestion for this?对此有何建议? content-type should be application/x-www-form-urlencoded.内容类型应该是 application/x-www-form-urlencoded。

    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();


In another form:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 x-www-form-urlencoded 正文发送 post 请求 - How to send post request with x-www-form-urlencoded body 如何使用Android批注和RestTemplate在POST请求的正文中发送x-www-form-urlencoded - How to send x-www-form-urlencoded in a body of POST request using android annotations and resttemplate FeignClient 使用 application/x-www-form-urlencoded 正文创建 POST - FeignClient create POST with application/x-www-form-urlencoded body 如何以 application/x-www-form-urlencoded 在 restTemplate 中发送正文 - How to send body in restTemplate as application/x-www-form-urlencoded 在正文中使用 application/x-www-form-urlencoded 的 400 错误白色 Java Post - 400 error white Java Post using application/x-www-form-urlencoded in body 如何在 HTTPURLConnection 中以 application/x-www-form-urlencoded 格式向 POST 调用添加正文 - How to add a body to POST call in HTTPURLConnection in application/x-www-form-urlencoded format POST x-www-form-urlencoded - POST x-www-form-urlencoded 未找到Form&application / x-www-form-urlencoded的Body Writer - Body Writer not found for Form & application/x-www-form-urlencoded POST 调用接受 x-www-form-urlencoded 但拒绝 JSON - POST call accepts x-www-form-urlencoded but refuses JSON 使用x-www-form-urlencoded的Jersey客户端发布请求失败 - Jersey client Post Request with x-www-form-urlencoded Fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM