This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: HTTP PUT request Example
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.2, Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: orsenthil Nosy List: ezio.melotti, karlcow, orsenthil, pitrou, python-dev
Priority: normal Keywords: patch

Created on 2013-02-27 08:59 by orsenthil, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
http_put_example.patch orsenthil, 2013-02-27 08:59 review
issue17307-2.patch orsenthil, 2013-02-28 04:23 review
Messages (7)
msg183124 - (view) Author: Senthil Kumaran (orsenthil) * (Python committer) Date: 2013-02-27 08:59
I think an explicit HTTP put request example in the docs may help. Previously, I thought the POST example was enough as PUT is very similar, but given the folks are looking for it, an example of how to do a PUT request using httplib may be helpful.

Here is patch attached for review.
msg183148 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-02-27 12:35
Shouldn't that be done with urllib?
msg183203 - (view) Author: Senthil Kumaran (orsenthil) * (Python committer) Date: 2013-02-28 04:23
Ezio. yes in 3.3 onwards it should be done in urllib.request. I was having 2.7 docs in my mind, but I ended up writing for latest with the intention to backport. I have updated the patch to include both ways, while backporting to 2.7 and 3.2, plan to include only the httplib ones.
msg183230 - (view) Author: karl (karlcow) * Date: 2013-02-28 20:20
Sentil:

About the PUT/POST, I would say:

    A POST and PUT methods differs only by the 
    intent of the enclosed representation. In the 
    case of a POST, the target resource (URI) on 
    the server decides what is the meaning of the 
    enclosed representation in the POST request.
    In a PUT request, the enclosed representation 
    is meant to replace the state of the target 
    resource (URI) on the server.
    It is why PUT is idempotent.

About the code:

* http.client
I would remove the following comment, because the term "file" is confusing in HTTP terms.

   # This will create a resource file with contents of BODY

or I would say more exactly


   # This creates an HTTP message 
   # with the content of BODY as the enclosed representation 
   # for the resource http://localhost:8080/foobar

   >>> import http.client
   >>> BODY = "Some data"
   >>> conn = http.client.HTTPConnection("localhost", 8080)
   >>> conn.request("PUT", "/foobar", BODY) # The actual PUT Request
   >>> response = conn.getresponse()
   >>> print(resp.status, response.reason)

Maybe it would be good to display the message which is sent so people can understand what goes on the wire.

* urllib
the client code for urllib doesn't create challenge, I would had a content-type but no hard feeling about it. On the other hand the server code makes me a bit uncomfortable. It sets people into believing that this is the way you should reply to a PUT which is not really the case.

1. If the resource was not existing and has been successfully created, the server MUST answer 204.
2. if the resource already exists and has been successfully replaced/modified, then the server SHOULD answer either 200 or 204 (depending on the design choice)

There are plenty of other cases depending on the constraints.  See for the details http://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-22#section-4.3.4

If we keep the server code, I would be willing to have a note saying that it is not usable as-is in production code. 

Does that make sense? :)
msg184068 - (view) Author: Senthil Kumaran (orsenthil) * (Python committer) Date: 2013-03-13 04:47
Karl, I am getting back to this. Thanks for your review comments on the patch.

1. Agree with your explanation on HTTP PUT. I shall modify the comment.
2. I think, it is okay to remove the HTTP PUT server example. I wrote it for test purposes and I think, it is a good idea to publish the test servers in the python docs.
msg184110 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-03-13 20:40
New changeset 4edde40afee6 by Senthil Kumaran in branch '2.7':
#17307 - Example of HTTP PUT Request using httplib
http://hg.python.org/cpython/rev/4edde40afee6

New changeset d4ab6556ff97 by Senthil Kumaran in branch '3.2':
#17307 - Example of HTTP PUT Request using http.client
http://hg.python.org/cpython/rev/d4ab6556ff97

New changeset 9941a9bbfeb6 by Senthil Kumaran in branch '3.3':
#17307 - merge from 3.2
http://hg.python.org/cpython/rev/9941a9bbfeb6

New changeset 57e5409a998e by Senthil Kumaran in branch 'default':
#17307 - merge from 3.3
http://hg.python.org/cpython/rev/57e5409a998e
msg184111 - (view) Author: Senthil Kumaran (orsenthil) * (Python committer) Date: 2013-03-13 20:41
Thanks for the review, Karl. Made the doc changes in all codelines.
History
Date User Action Args
2022-04-11 14:57:42adminsetgithub: 61509
2013-03-13 20:41:22orsenthilsetstatus: open -> closed
resolution: fixed
messages: + msg184111

stage: patch review -> resolved
2013-03-13 20:40:30python-devsetnosy: + python-dev
messages: + msg184110
2013-03-13 04:47:18orsenthilsetmessages: + msg184068
2013-02-28 20:20:12karlcowsetnosy: + karlcow
messages: + msg183230
2013-02-28 04:23:12orsenthilsetfiles: + issue17307-2.patch

messages: + msg183203
2013-02-27 12:35:08ezio.melottisettype: enhancement
messages: + msg183148
components: + Documentation
2013-02-27 08:59:47orsenthilcreate