如果上传多个文件时一个文件验证失败,相应的响应和 HTTP 状态代码是什么
What is the appropriate response and HTTP status code if one file validation fails when uploading multiple files
有一个端点可以为最多 5 个 csv 文件执行文件上传。当用户在未发送任何文件的情况下访问端点时,请求失败。用户可以只发送一个文件或多个文件,但不能超过五个。如果用户上传了三个文件,其中两个是csv文件,一个是txt文件,服务器应该给出什么合适的响应?
服务器是否会 return 200 或 400 状态代码和一条消息,说明文件上传成功和上传失败。或者 return 一个 400 状态代码并拒绝所有上传的文件是否更合适,因为至少有一个文件的格式不正确?
我考虑过 returning 一个 200 状态代码和一条消息,说明已成功上传文件,同时说明其他文件未上传的原因。如果 none 上传文件的格式正确,我还会 return 400 状态代码。大家怎么看?
The response MAY be used in success, partial success and also in failure situations.
这取决于用例和您想提供给的用户体验。
Would the server return a 200 or 400 status code and a message stating the files that were successfully uploaded and those not successful...
考虑使用 MULTI-STATUS 207
HTTP 代码。
以下是 httpstatuses.com 对此的评价:
A Multi-Status response conveys information about multiple resources in situations where multiple status codes might be appropriate.
如果你走这条路,那么 return 207 响应和响应正文中的详细信息列表就完全没问题了。
这种方法比下面的替代方法复杂一些,但我相信它会为通过 HTML 表单上传文件的用户提供更好的用户体验。
...would it be more appropriate to return a 400 status code and reject all uploaded files since at least one file wasn't in the right format?
这个实现起来比较简单。当自动系统将文件发送到您的网络服务器时,这种方法可能更可取。
有一个端点可以为最多 5 个 csv 文件执行文件上传。当用户在未发送任何文件的情况下访问端点时,请求失败。用户可以只发送一个文件或多个文件,但不能超过五个。如果用户上传了三个文件,其中两个是csv文件,一个是txt文件,服务器应该给出什么合适的响应? 服务器是否会 return 200 或 400 状态代码和一条消息,说明文件上传成功和上传失败。或者 return 一个 400 状态代码并拒绝所有上传的文件是否更合适,因为至少有一个文件的格式不正确?
我考虑过 returning 一个 200 状态代码和一条消息,说明已成功上传文件,同时说明其他文件未上传的原因。如果 none 上传文件的格式正确,我还会 return 400 状态代码。大家怎么看?
The response MAY be used in success, partial success and also in failure situations.
这取决于用例和您想提供给的用户体验。
Would the server return a 200 or 400 status code and a message stating the files that were successfully uploaded and those not successful...
考虑使用 MULTI-STATUS 207
HTTP 代码。
以下是 httpstatuses.com 对此的评价:
A Multi-Status response conveys information about multiple resources in situations where multiple status codes might be appropriate.
如果你走这条路,那么 return 207 响应和响应正文中的详细信息列表就完全没问题了。
这种方法比下面的替代方法复杂一些,但我相信它会为通过 HTML 表单上传文件的用户提供更好的用户体验。
...would it be more appropriate to return a 400 status code and reject all uploaded files since at least one file wasn't in the right format?
这个实现起来比较简单。当自动系统将文件发送到您的网络服务器时,这种方法可能更可取。