简体   繁体   English

我应该将我的 REST API 设为 PUT 还是 POST 以进行文件上传?

[英]Should I make my REST API a PUT or POST for a file upload?

I am working on an API that will take an xml file, parse the file to a DTO and return the results in the form of JSON.我正在研究 API,它将采用 xml 文件,将文件解析为 DTO 并以 JSON 的形式返回结果。 Technically you can say the API's purpose is to only parse the file and return the results in a "nice" format.从技术上讲,您可以说 API 的目的是仅解析文件并以“不错”的格式返回结果。 I'm not sure whether or not it makes more sense to make this a POST vs PUT api?我不确定将其设为 POST 与 PUT api 是否更有意义? I know POST is usually for create and PUT is for update.我知道 POST 通常用于创建,而 PUT 用于更新。 In this case I'm not really creating or updating anything.在这种情况下,我并没有真正创建或更新任何东西。

If you aren't doing something standard, it's okay to use POST .如果您没有做一些标准的事情,可以使用 POST

POST serves many useful purposes in HTTP, including the general purpose of “this action isn't worth standardizing.” POST 在 HTTP 中有许多有用的用途,包括“此操作不值得标准化”的一般用途。

The purpose of PUT is to replace what exists at your target url. PUT的目的是替换目标 url 中存在的内容。

So if you do a PUT request with a file on /foo/bar , then I would expect a GET request afterwards on /foo/bar to return that same file.因此,如果您使用/foo/bar上的文件执行 PUT 请求,那么我希望之后/foo/bar上的GET请求会返回相同的文件。

It doesn't sound like that's what you're doing, so POST is likely the right choice.这听起来不像你正在做的,所以POST可能是正确的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM