英文:
How to send file and object using postman?
问题
我有一个端点,用于添加文件和对象。以下是函数参数:
@RequestMapping(
value = "/request",
method = RequestMethod.POST,
consumes = {"multipart/form-data"}
)
@ResponseBody
@Transactional
public ResponseEntity<Object> requestLicense(
@RequestPart("properties") @Valid LicenseRequest request,
@RequestPart("file1") @Valid @NotNull @NotBlank MultipartFile file1,
@RequestPart("file2") @Valid @NotNull @NotBlank MultipartFile file2
) {
...
}
我想使用 Postman 发送正确的 POST 请求,但我不知道如何操作。
如何进行操作,还是说这是不可能的?
英文:
I have an endpoint which is adding files and object. Here are the function parameters:
@RequestMapping(
value = "/request",
method = RequestMethod.POST,
consumes = {"multipart/form-data"}
)
@ResponseBody
@Transactional
public ResponseEntity<Object> requestLicense(
@RequestPart("properties") @Valid LicenseRequest request,
@RequestPart("file1") @Valid @NotNull @NotBlank MultipartFile file1,
@RequestPart("file2") @Valid @NotNull @NotBlank MultipartFile file2
) {
...
}
And I would like to send correct post method using postman but I do not know how to do it.
How do I do it or it is impossible?
答案1
得分: 1
是的,这是可能的,而且你自己已经做了很多。
只需提醒你,在你的终端节点有两个文件,它们都用 @NotNull
进行了注释,因此你需要发送两个具有指定名称的文件(在你的情况下是 file1
和 file2
)。
这些名称应该恰好位于你的表单数据的键部分。
可以看一下这个示例:
英文:
yes it is possible and you have done lots of it yourself.
just remind that you have two files in your endpoint's requirements and they both are annotated with @NotNull
, so you need to send two files with the specified names (which are file1
and file2
in your case).
these names should exactly be in the key part of your form-data.
have a look on this:
答案2
得分: 1
根据Majid_Roustaei的回答提到的,传递给该方法的有效帖子需要包含两个单独的文件参数,即file1
和file2
,以及properties
参数。
这就是多部分表单数据的目的。
你已经接近成功了!
英文:
So as mentioned by Majid_Roustaei answer, a valid post to the method needs to contain 2 separate files parameters, namely file1
and file2
plus the properties
parameter.
It is the purpose of the Multipart Form-Data.:
You are almost there !
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论