HTTP PUT 文件和数据

发布于 2024-12-06 07:33:46 字数 190 浏览 0 评论 0原文

在我的 RESTful API 中,我有一张用户可以通过 PUT 请求更新的图片。

假设每张图片都有一个用户可以指定的自定义名称,以便他们可以轻松浏览它们。

我想做的是发送一个包含更新文件和图片新名称的 put 请求,但我不确定如何让 PHP 在从 php:// 读取时将更新文件与新名称分开输入

有人知道该怎么做吗?

In my RESTful API say I have a picture the user can update via a PUT request.

And let's say each of these pictures has a custom name the user can assign them so they can easily browse them.

What I'd like to do is send a put request that contains the updated file AND the new name for the picture, but I'm not sure how to have PHP separate the updated file from the new name when reading from php://input

Does anyone know how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

全部不再 2024-12-13 07:33:46

将标量参数(即旧名称、新名称)放入查询字符串中。

Place scalar parameters (i. e. old name, new name) into the query string.

溺孤伤于心 2024-12-13 07:33:46

看看 AtomPub ( https://www.rfc-editor.org/rfc/rfc5023< /a> ),尤其是编辑媒体链接。它并不能完全满足您的需求,但也许您可以调整它。

从语义上讲,将多部分文档放入原子条目应该是可以的,其中多部分的第一部分是用于更新标题的原子条目 XML。该条目的内容元素可以使用 cid: URI ( https://www.rfc-editor.org/rfc/rfc2392

Slug 标头(也在 RFC 5023 中)也可能是一个开始去调查。

您可能会搜索有关 Content-Disposition: 和 Title: 的一些较旧的标题。

另一种选择是提出一个具有适当语义的新资源,然后对其发布或修补多部分或结构化文档。

Have a look at AtomPub ( https://www.rfc-editor.org/rfc/rfc5023 ), especially edit-media links. It does not quite give you what you want, but maybe you can adapt it.

It should be semantically ok to PUT a multipart doc to an atom entry where the first part of the multipart is an atom entry XML to update the title. The content element of that entry could point to the second multipart part (the image data) using a cid: URI ( https://www.rfc-editor.org/rfc/rfc2392 )

The Slug header (also in RFC 5023) might also be a start to investigate.

There might also be some older headers around Content-Disposition: and Title: you might search for.

Another option is to just come up with a new resource that has the appropriate semantics and POST or PATCH a multipart or structured doc to that.

Jan

诗酒趁年少 2024-12-13 07:33:46

为什么不使用 url 作为文件名?

PUT /images/new_image.jpg

Why don't you use the url for the filename?

PUT /images/new_image.jpg
半葬歌 2024-12-13 07:33:46

如果您希望在同一请求正文中包含多种内容类型,正确的格式是使用多部分 mime 将它们全部包装到同一请求正文中。不过,在这种情况下,支持像多部分哑剧这样复杂的东西可能有点难以证明其合理性。

The correct format if you want to have multiple content types in the same request body is to use multipart mime to wrap them all into the same request body. Supporting something as complicated as multipart mime might be a bit hard to justify in this case though.

那伤。 2024-12-13 07:33:46

经过考虑,我认为您需要的是带有 multipart/form-data 的 POST 请求。这允许文件和一些标量数据项,而无需对文件数据进行 Base64 或 URLEncode 的开销。

Upon consideration, I think what you need is a POST request with multipart/form-data. This allows for both file and some scalar data items, without the overhead of Base64 or URLEncode on the file data.

蝶…霜飞 2024-12-13 07:33:46

使用 SLUG 标头来执行此操作:

换句话说,Slug 标头为客户端提供了一种为新创建的资源建议 URI 的方法。

URI只能使用有限的字符集,如果Slug使用合法URI字符集之外的字符,那么服务器必须对这些字符进行转义。

两个或多个客户端可能会尝试同时创建具有相同 Slug 的资源,服务器必须为每个资源提供唯一的 URI,因此服务器可能会选择用附加字符来装饰 slug 以确保每个资源都是唯一的命名。

参考

Use the SLUG header to do this:

In other words the Slug header provides a means for a client to suggest the URI for a newly created resource.

URIs can only use a limited set of characters, if the Slug uses characters outside the legal URI character set, then the server must escape those characters.

Two or more clients might attempt to create a resource with the same Slug at the same time, the server must give each resource a unique URI, so the server may choose to decorate the slug with additional characters to ensure each resource is uniquely named.

References

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文