简体   繁体   English

HTTP PUT文件和数据

[英]HTTP PUT file and data

In my RESTful API say I have a picture the user can update via a PUT request. 在我的RESTful API中说我有一张图片,用户可以通过PUT请求更新。

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 我想做的是发送包含更新文件的放置请求和图片的新名称,但是我不确定如何在从php://读取时将PHP从新名称中分离出更新的文件输入

Does anyone know how to do this? 有谁知道如何做到这一点?

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

Have a look at AtomPub ( http://tools.ietf.org/html/rfc5023 ), especially edit-media links. 看看AtomPub( http://tools.ietf.org/html/rfc5023 ),特别是编辑媒体链接。 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. 将多部分文档输入到原子条目应该在语义上是可行的,其中多部分的第一部分是用于更新标题的原子条目XML。 The content element of that entry could point to the second multipart part (the image data) using a cid: URI ( http://tools.ietf.org/html/rfc2392 ) 该条目的内容元素可以使用cid:URI指向第二个多部分(图像数据)( http://tools.ietf.org/html/rfc2392

The Slug header (also in RFC 5023) might also be a start to investigate. Slug标题(也在RFC 5023中)也可能是一个开始调查。

There might also be some older headers around Content-Disposition: and Title: you might search for. Content-Disposition可能还有一些较旧的标题:和标题:您可以搜索。

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. 另一种选择是提出一个具有适当语义的新资源,以及POST或PATCH多部分或结构化文档。

Jan 一月

Use the SLUG header to do this: 使用SLUG标头执行此操作:

In other words the Slug header provides a means for a client to suggest the URI for a newly created resource. 换句话说,Slug标头为客户端提供了一种为新创建的资源建议URI的方法。

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. URI只能使用一组有限的字符,如果Slug使用合法URI字符集之外的字符,则服务器必须转义这些字符。

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. 两个或多个客户端可能同时尝试使用相同的Slug创建资源,服务器必须为每个资源提供唯一的URI,因此服务器可以选择使用其他字符装饰slug以确保每个资源具有唯一名称。

References 参考

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

PUT /images/new_image.jpg

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. 如果要在同一请求主体中具有多个内容类型,则使用正确的格式是使用多部分mime将它们全部包装到同一请求主体中。 Supporting something as complicated as multipart mime might be a bit hard to justify in this case though. 在这种情况下,支持像multipart mime这样复杂的东西可能有点难以证明。

Upon consideration, I think what you need is a POST request with multipart/form-data . 经过考虑,我认为你需要的是一个带有multipart/form-data的POST请求。 This allows for both file and some scalar data items, without the overhead of Base64 or URLEncode on the file data. 这允许文件和一些标量数据项,而不会在文件数据上产生Base64或URLEncode的开销。

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

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