HTTP PUT 文件和数据
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将标量参数(即旧名称、新名称)放入查询字符串中。
Place scalar parameters (i. e. old name, new name) into the query string.
看看 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
为什么不使用 url 作为文件名?
Why don't you use the url for the filename?
如果您希望在同一请求正文中包含多种内容类型,正确的格式是使用多部分 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.
经过考虑,我认为您需要的是带有
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.使用 SLUG 标头来执行此操作:
参考
Use the SLUG header to do this:
References