我可以不带 ID 就 PUT 吗? [英] Can I PUT without an ID?

查看:35
本文介绍了我可以不带 ID 就 PUT 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个 API Rest 服务,允许用户将文件上传到服务器.

I'm designing an API Rest service that allows the user to upload a file to the server.

我认为这是一个 PUT 请求,它会转到服务器/资源/ID 并在 json 请求正文中将文件作为 base64.

I'm thinking this is a PUT request and it would go to server/resource/ID and have the file as base64 in the json request body.

我的问题是关于这个 ID.在我的脑海中,我正在将文件传递给服务器,服务器应该负责存储该文件并生成一个唯一的 ID 以便稍后检索它,然后将该 ID 返回给状态为 ok 的客户端.

My question is regarding this ID. In my head, I'm passing the file to the server and the server should be in charge of storing that file and generating a unique ID to retrieve it later, and then return this ID to the client with an ok status.

所以我正在考虑这样做,在没有 ID 的情况下将其发送到服务器/资源,但是这样可以还是设计不好?

So I'm thinking about doing that, sending it to server/resource, without the ID, but is this ok or is it bad design?

推荐答案

没有.PUT 的意思是创建或更新",应该带有一个明确的 ID.POST 适用于创建新事物.

No. PUT means "create or update", and should come with an explicit ID. POST is suitable for creating something new.

另见:REST 中的 PUT 与 POST

这篇关于我可以不带 ID 就 PUT 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆