【发布时间】:2020-02-25 13:02:43
【问题描述】:
我的 Springboot 后端休息 API 需要一个带有 2 个参数的 POST 请求,如下所示:-
@PostMapping(path = "/profile" , consumes = {"multipart/form-data"})
public ResponseEntity<?> addOrUpdateProfile(@RequestPart("profile") Profile profile, @RequestParam("file") MultipartFile file) {
System.out.println("file name:"+file.getOriginalFilename()+" file is here. Save it");
profileService.saveOrUpdateExpense(profile);
return new ResponseEntity<>("Expense added succcessfully", HttpStatus.OK);
}
我可以通过 curl 测试后端代码为:-
curl -X POST -H 'Content-Type: multipart/form-data' -F profile='{ "displayName": "ma ma", "birthDate": "1999-01-01", "gender": "male"};type=application/json' -F file=@'/home/dev/Downloads/file.pdf;type=application/octet-stream' http://localhost:8080/profile
现在我试图在我的反应前端通过 axios 发出相同的 POST 请求。我是 ReactJs 的新手,我不知道该怎么做。请让我知道该怎么做?
【问题讨论】: