如何在 Postman 中发送文件和对象?

huangapple go评论113阅读模式
英文:

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 = &quot;/request&quot;,
    method = RequestMethod.POST,
    consumes = {&quot;multipart/form-data&quot;}
)
@ResponseBody
@Transactional
public ResponseEntity&lt;Object&gt; requestLicense(
    @RequestPart(&quot;properties&quot;) @Valid LicenseRequest request,
    @RequestPart(&quot;file1&quot;) @Valid @NotNull @NotBlank MultipartFile file1,
    @RequestPart(&quot;file2&quot;) @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.
如何在 Postman 中发送文件和对象?

How do I do it or it is impossible?

答案1

得分: 1

是的,这是可能的,而且你自己已经做了很多。

只需提醒你,在你的终端节点有两个文件,它们都用 @NotNull 进行了注释,因此你需要发送两个具有指定名称的文件(在你的情况下是 file1file2)。

这些名称应该恰好位于你的表单数据的键部分。

可以看一下这个示例:

如何在 Postman 中发送文件和对象?

英文:

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:

如何在 Postman 中发送文件和对象?

答案2

得分: 1

根据Majid_Roustaei的回答提到的,传递给该方法的有效帖子需要包含两个单独的文件参数,即file1file2,以及properties参数。

这就是多部分表单数据的目的。

如何在 Postman 中发送文件和对象?

你已经接近成功了!

英文:

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.:

如何在 Postman 中发送文件和对象?

You are almost there !

huangapple
  • 本文由 发表于 2020年8月20日 14:09:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63499239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定