servicecomb-java-chassis icon indicating copy to clipboard operation
servicecomb-java-chassis copied to clipboard

@RequestPart cannot mapping string value,Required request part 'hostname' is not present

Open RayTigerZ opened this issue 3 years ago • 5 comments

ServerCode

@PostMapping("/uploadLog")
 public String uploadAnalysisLog(@RequestPart String hostname, @RequestPart MultipartFile runLog)

ClientCode

MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
form.add("runLog", fileResource);
form.add("hostname", hostname);
HttpEntity<MultiValueMap<String, Object>> body = new HttpEntity<>(form, headers);
RestTemplate restTemplate = HttpRestClient.getRestTemplateCse();
String result = restTemplate.postForObject(UPLOAD_LOG_URL, body, String.class);

RayTigerZ avatar Oct 21 '22 05:10 RayTigerZ

Check developer guide, seems not support this usage: https://servicecomb.apache.org/references/java-chassis/zh_CN/general-development/file-upload.html

But jaxrs @FormParam can be used:

  @Path("/upload2")
  @POST
  @Produces(MediaType.TEXT_PLAIN)
  public String fileUpload2(@FormParam("file1") Part file1, @FormParam("message") String message) 

liubao68 avatar Oct 21 '22 06:10 liubao68

sending request in Service Center is worked,but not work with CSERestTemplate

RayTigerZ avatar Oct 21 '22 06:10 RayTigerZ

What do you mean by sending request in Service Center is worked ? Can you add an example?

liubao68 avatar Oct 21 '22 06:10 liubao68

network info in chrome Request URL: https://*/cse/service/uploadLog

payload: ------WebKitFormBoundary6XjKBy4C1GAEaRpH Content-Disposition: form-data; name="hostname"

7777777777 ------WebKitFormBoundary6XjKBy4C1GAEaRpH Content-Disposition: form-data; name="runLog"; filename="Bi.log" Content-Type: application/octet-stream

------WebKitFormBoundary6XjKBy4C1GAEaRpH--

response: uploadAnalysisLog succeed

RayTigerZ avatar Oct 21 '22 06:10 RayTigerZ

seems you missed content-type, and I test it works

see https://github.com/apache/servicecomb-java-chassis/pull/3419

liubao68 avatar Oct 21 '22 09:10 liubao68