3

This is my first question in this forum. I want to POST(using PUT Method) the data using multipart form data. I found the code for Multipart Post Form Data code in doughellmann.com.It works fine for post data. I ant to add my parameters in PUT Method and i tried it. it does not return back .... Could you please help on this guys....

Code

# This Script is used for upload API Calls
# This script would be inherited under Device API
# 
import itertools
import mimetools
import mimetypes
from cStringIO import StringIO
import urllib
import urllib2
class MultiPartForm(object):
 """Accumulate the data to be used when posting a form."""
 def __init__(self):
 self.form_fields = []
 self.files = []
 self.boundary = mimetools.choose_boundary()
 return
 def get_content_type(self):
 return 'multipart/form-data; boundary=%s' % self.boundary
 def add_field(self, name, value):
 """Add a simple field to the form data."""
 self.form_fields.append((name, value))
 return
 def add_file(self, fieldname, filename, fileHandle, mimetype=None):
 """Add a file to be uploaded."""
 body = fileHandle.read()
 if mimetype is None:
 mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream'
 self.files.append((fieldname, filename, mimetype, body))
 return
 def __str__(self):
 """Return a string representing the form data, including attached files."""
 # Build a list of lists, each containing "lines" of the
 # request. Each part is separated by a boundary string.
 # Once the list is built, return a string where each
 # line is separated by '\r\n'. 
 parts = []
 part_boundary = '--' + self.boundary
 # Add the form fields
 parts.extend(
 [ part_boundary,
 'Content-Disposition: form-data; name="%s"' % name,
 '',
 value,
 ]
 for name, value in self.form_fields
 )
 # Add the files to upload
 parts.extend(
 [ part_boundary,
 'Content-Disposition: file; name="%s"; filename="%s"' % \
 (field_name, filename),
 'Content-Type: %s' % content_type,
 '',
 body,
 ]
 for field_name, filename, content_type, body in self.files
 )
 # Flatten the list and add closing boundary marker,
 # then return CR+LF separated data
 flattened = list(itertools.chain(*parts))
 flattened.append('--' + self.boundary + '--')
 flattened.append('')
 return '\r\n'.join(flattened)
if __name__ == '__main__':
 # Create the form with simple fields
 form = MultiPartForm()
 form.add_field('firstname', 'Doug')
 form.add_field('lastname', 'Hellmann')
 # Add a fake file
 form.add_file('biography', 'bio.txt', 
 fileHandle=StringIO('Python developer and blogger.'))
 # Build the request
 request = urllib2.Request('http://192.1681.2/1.0/api/filecontents')
 request.add_header('User-agent', 'PyMOTW (http://www.doughellmann.com/PyMOTW/)')
 body = str(form)
 request.add_header('Content-type', form.get_content_type())
 request.add_header('Content-length', len(body))
 request.add_data(body)
 regparams={'restMethod' : 'PUT' ,'HTTP_RANGE' :'10-20' }
 print
 print 'OUTGOING DATA:'
 print request.get_data()
 params = urllib.urlencode(regparams)
 print
 print 'SERVER RESPONSE:'
 print urllib2.urlopen(request,params).read()
user225312
133k71 gold badges177 silver badges183 bronze badges
asked Jan 19, 2011 at 19:00
2
  • 4
    If you're using PUT, then you aren't really "POSTing" the data. Commented Jan 19, 2011 at 19:05
  • Hi, I am using to post the data to device(upload the image from PC to mobiledevie). The POST is working fine. I dont know how to use PUT method. Could you please help on this. Commented Jan 21, 2011 at 0:43

1 Answer 1

2

You should consider using the requests module:

with open("report.xls", "rb") as report:
 requests.post("http://httpbin.org/post", files={'file': report})
answered Oct 31, 2015 at 18:10
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.