简体   繁体   中英

HTTP PUT file and data

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?

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

Have a look at AtomPub ( http://tools.ietf.org/html/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 ( http://tools.ietf.org/html/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

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

为什么不使用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. Supporting something as complicated as multipart mime might be a bit hard to justify in this case though.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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