Obto
Obto

Reputation: 1417

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?

Upvotes: 0

Views: 332

Answers (6)

Paul Sweatte
Paul Sweatte

Reputation: 24617

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

Upvotes: 0

Seva Alekseyev
Seva Alekseyev

Reputation: 61341

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.

Upvotes: 0

Jan Algermissen
Jan Algermissen

Reputation: 4978

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

Upvotes: 1

VoxPelli
VoxPelli

Reputation: 2877

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.

Upvotes: 0

Seva Alekseyev
Seva Alekseyev

Reputation: 61341

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

Upvotes: 1

Evert
Evert

Reputation: 99505

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

PUT /images/new_image.jpg

Upvotes: 0

Related Questions