[Twisted-Python] twisted.web.client POST data
tarjei
tarjei at nu.no
Mon Sep 15 05:35:56 MDT 2008
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Vasil Vangelovski wrote:
> I'm trying to send a post request like so
> getPage('http://localhost/test/test/', method='POST' postdata='test=asdf')
>
> But the server script doesnt get any post arguments.
> I was looking at the code for web.client and cant see where im doing
> wrong here.
> Any ideas what might be the problem? I'm using an svn checkout of Twisted.
Try:
getPage('http://localhost/test/test/?test=asdf', method='POST' postdata='')
If you want to use postdata, you'll have to encode it yourself. AFAIK
there is no method for doing this in twisted.web. I've used the
following which is based on[1]:
def encode_multipart_formdata(fields, files, content_type=
'application/octet-stream'):
"""
fields is a sequence of (name, value) elements for regular form fields.
files is a sequence of (name, filename, value) elements for data to
be uploaded as files
content_type is the contentype to send
Return (content_type, body) ready for httplib.HTTP instance
"""
BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
CRLF = '\r\n'
L = []
for (key, value) in fields:
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s"' % key)
L.append('')
L.append(value)
for (key, filename, value) in files:
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s";
filename="%s"' % (key, filename))
L.append('Content-Type: %s' % content_type)
L.append('')
L.append(value)
L.append('--' + BOUNDARY + '--')
L.append('')
body = CRLF.join(L)
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
return content_type, body
This code makes it possible to also add files to the post.
1. http://code.activestate.com/recipes/146306/
Regards,
Tarjei
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFIzkicYVRKCnSvzfIRArU/AJ0XG6qKXbRYxj1fnwE3sm+xh0tJ6wCdEoYL
XqwNVrfspaKo2r76hSug01I=
=UpmN
-----END PGP SIGNATURE-----
More information about the Twisted-Python
mailing list