我设置标题的方式如下:
import org.springframework.web.reactive.function.client.WebClient;
WebClient webClient = WebClient.create();
webClient.post().uri(url)
.headers(httpHeaders -> httpHeaders.setAll(headersMap))
.body(BodyInserters.fromFormData(HelperMethods.mapToMultiValueMap(body))).exchange();
它适用于某些服务,但在我添加自定义标题基础的地方遇到了问题。
为了设置内容类型,我在 headersMap 的标题中添加了以下内容(headersMap 是 map ):(“Content-Type”,“application/json”)
但它给了我错误:“HTTP header 行 ["Content-Type": "application/json"] 不符合 RFC 7230 并且已被忽略”
这可能是什么原因造成的?我尝试发送内容类型,如:("content-type", "application/json"),但错误是一样的。
我无法使用“.contentType()”在请求中设置 header ,因为 header 的数量是在 headersMap 中动态设置的变量。
最佳答案
您正在发送内容类型指向 json 的表单数据(通常为 Content-Type: multipart/form-data
) - 发送正确的 JSON 或更改您的标题以适合表单数据。
关于java - Spring Webflux Webclient |内容类型标题设置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55451598/