Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP Interface client cannot send URI query parameter with multipart request #30520

Closed
zvae opened this issue May 23, 2023 · 1 comment
Closed
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@zvae
Copy link

zvae commented May 23, 2023

Problem:
I have an interface that needs to pass a file and a uri parameter, I use WebClient HTTP Interface , like this:

@PostExchange(value = "/fileUpload",contentType = MediaType.MULTIPART_FORM_DATA_VALUE)
BaseEntity upload( @RequestPart("file") HttpEntity param, @RequestParam("storageType") String storageType);

call this method,the server can not receive the file 。
in the code : org.springframework.web.service.invoker.HttpRequestValues.Builder#build

        if (!CollectionUtils.isEmpty(this.requestParams)) {

		boolean isFormData = (this.headers != null &&
				MediaType.APPLICATION_FORM_URLENCODED.equals(this.headers.getContentType()));

		if (isFormData) {
			Assert.isTrue(bodyValue == null && this.body == null, "Expected body or request params, not both");
			bodyValue = new LinkedMultiValueMap<>(this.requestParams);
		}
		else if (uri != null) {
			uri = UriComponentsBuilder.fromUri(uri)
					.queryParams(UriUtils.encodeQueryParams(this.requestParams))
					.build(true)
					.toUri();
		}
		else {
			uriVars = (uriVars.isEmpty() ? new HashMap<>() : uriVars);
			uriTemplate = appendQueryParams(uriTemplate, uriVars, this.requestParams);
		}
	}
	else if (this.multipartBuilder != null) {
		Assert.isTrue(bodyValue == null && this.body == null, "Expected body or request parts, not both");
		bodyValue = this.multipartBuilder.build();
	}

if the request have requestParams, the multipartBuilder won't handle it.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 23, 2023
@rstoyanchev rstoyanchev added the in: web Issues in web modules (web, webmvc, webflux, websocket) label May 24, 2023
@rstoyanchev
Copy link
Contributor

We can improve the logic to first make a decision if the request is multipart by checking the content-type and the presence of MultipartBodyBuilder, and if it is then prepare it as such.

When it comes to @RequestParam, the term request parameter comes from the Servlet API, and could mean a parameter from the query of the URI, from the body of a form data request, or from the parts of a multipart request. When handling a request it's possible to check all places, but when preparing a request it's less clear what it should be.

For a multipart request at least, we can expect use of @RequestPart if it needs to be in the body, but otherwise @RequestParam becomes a query parameter. For a form data request however, @RequestParam would have to remain as is, i.e. always in the body as there is no way to differentiate one from the other.

@rstoyanchev rstoyanchev added this to the 6.0.10 milestone May 24, 2023
@rstoyanchev rstoyanchev added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 24, 2023
@rstoyanchev rstoyanchev changed the title WebClient HTTP Interface cannot send uri parameter and body at the same time HTTP Interface client cannot send URI query parameter with multipart request May 24, 2023
@rstoyanchev rstoyanchev self-assigned this May 30, 2023
mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants