Transfer a file to httpserver via POST command from curl

Chris Angelico rosuav at gmail.com
Mon Dec 23 12:08:33 EST 2019


On Tue, Dec 24, 2019 at 2:44 AM Peter J. Holzer <hjp-python at hjp.at> wrote:
>
> On 2019-12-23 04:24:22 +1100, Chris Angelico wrote:
> > On Mon, Dec 23, 2019 at 4:20 AM <musbur at posteo.org> wrote:
> > > On Wed, 18 Dec 2019 04:52:33 +1100
> > > Chris Angelico <rosuav at gmail.com> wrote:
> > >
> > > > On Wed, Dec 18, 2019 at 4:45 AM <musbur at posteo.org> wrote:
> > > > > BTW, the canonical way to upload files via http is PUT, not POST.
> > > > > You might want to look into that, but here it is off-topic.
> > > >
> > > > Citation needed.
> > >
> > > https://tools.ietf.org/html/rfc2616#page-55
> >
> > Yes, that's the definition of PUT - but what does that mean in terms
> > of file uploads? POST is also valid. Look at its example usages.
>
> ACK. Also the first sentence reads:
>
> | The PUT method requests that the enclosed entity be stored under the
> | supplied Request-URI.
>
> I understand this to mean that when I send a
>
>     PUT https://example.com/foo/bar
>
> request and receive a 201 or 204 response, a subsequent
>
>     GET https://example.com/foo/bar
>
> will return the body I sent in the PUT request.
>
> This is fine if the client can determine the URI of the response.
>
> Quite often this not what you want. You want the client to supply the
> data but let the server determine what to do with it. For example, the
> server might just number the uploaded files (or use a timestamp or a
> hash). Or the uploaded file may not be available under any URL at all,
> but is used to modify other resources and then discarded.
>
> In these cases PUT is inappropriate and POST is appropriate.

Your understanding is exactly correct. PUT is appropriate when you
already know the URI, POST is appropriate when you want to create a
brand new item. The latter is actually far more common.

> And sometimes the uploaded file is only part of the data (think of HTML
> forms with a file input field). Then POST is also the correct choice.

Right, although APIs will often work differently from forms. For
instance, the Twitch video uploading protocol starts with a simple
POST request with some metadata to create a video, followed by a
series of PUT requests to submit each section of the file, and finally
a POST to complete the upload. But that only works because all the PUT
requests use a predictable URI based on an upload token (created by
the initial POST request), which isn't a normal way to build a file
upload. (Twitch's video upload can take some pretty ginormous files.
You don't want a simple connection glitch to force you to reupload the
entire thing from scratch.)

But your analysis of POST vs PUT is absolutely correct. File uploads
can be done either way, but only use PUT if you know the URI that the
object is to live at, which usually means that you can subsequently
GET that and retrieve your original file.

ChrisA


More information about the Python-list mailing list