Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8821119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:46:28+00:00 2026-06-14T05:46:28+00:00

params = {‘file’: open(test.txt, rb), ‘name’: ‘upload test’} datagen, headers = poster.encode.multipart_encode(params) request =

  • 0
params = {'file': open("test.txt", "rb"), 'name': 'upload test'}
datagen, headers = poster.encode.multipart_encode(params)
request = urllib2.Request(upload_url, datagen, headers)
result = urllib2.urlopen(request)

I use poster library to POST for HTTP. It works well. I’m satisfied with that.

But I want to try something. As you see above, to send file data, I have to OPEN a file. But is there any way not to make a real file to do that? We can use a STREAM, like StringIO, to deal with data like a file, right? But, I don’t know about poster deeply. So, I want to know the method to use a STREAM with poster.

ADDED

Actually, I tried to POST image data. I wrote this below

from PyQt4 import QtCore, QtGui
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2, os

register_openers()
app = QtGui.QApplication(sys.argv)
pixmap = QtGui.QPixmap("c:/test_img.png")
byte_array = QtCore.QByteArray()
buffer = QtCore.QBuffer(byte_array)
buffer.open(QtCore.QIODevice.WriteOnly)
pixmap.save(buffer, "PNG")
from cStringIO import StringIO
datagen, headers = multipart_encode({"image": StringIO(str(byte_array.toBase64()))})
request = urllib2.Request(upload_url, datagen, headers)
_rnt = urllib2.urlopen(request)

But, I receive this error:

Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    _rnt = urllib2.urlopen(request)
  File "C:\Python26\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python26\lib\urllib2.py", line 397, in open
    response = meth(req, response)
  File "C:\Python26\lib\urllib2.py", line 510, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python26\lib\urllib2.py", line 435, in error
    return self._call_chain(*args)
  File "C:\Python26\lib\urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "C:\Python26\lib\urllib2.py", line 518, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 500: Internal Server Error
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:46:30+00:00Added an answer on June 14, 2026 at 5:46 am

    The file param is where you pass the file object. So, what happens if you pass a file-like object instead?

    >>> params = {'file': cStringIO.StringIO('upload test data'), 'name': 'upload test'}
    >>> datagen, headers = poster.encode.multipart_encode(params)
    >>> headers
    {'Content-Length': '317', 'Content-Type': 'multipart/form-data; boundary=0c56082b1e134424a918b2b083391467'}
    

    Looks like it worked.

    What does the documentation say?

    Values are either strings parameter values, or file-like objects to use as the parameter value. The file-like objects must support .read() and either .fileno() or both .seek() and .tell().

    So, you can use StringIO objects because they support seek() and tell().

    But you don’t have to. You should be able to just use the raw string. Let’s try it and see:

    >>> params = {'file': 'upload test data', 'name': 'upload test'}
    >>> datagen, headers = poster.encode.multipart_encode(params)
    >>> headers
    {'Content-Length': '317', 'Content-Type': 'multipart/form-data; boundary=0c56082b1e134424a918b2b083391467'}
    

    Look at that, the documentation is correct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

post '/upload' do unless params[:file] && (tmpfile = params[:file][:tempfile]) && (name = params[:file][:filename]) return
System.Web.HttpContext.Current.Request.Params seems to return way too many params, including headers, etc... How can I
Request[key] vs Request.Params[key] vs Request.QueryString[key] Which method do you seasoned programmers use? and why?
The variable cherrypy.request.params as it is described in the API contains the query string
Auto-binding of request params seems like default behaviour, but I can't find a lot
I want to add Params in current request in OnActionExecuting() filter i tried this
I need to encode the params to ISOLatin which i intend to post to
if params[:body] is empty, read some file instead. is there a better way? default_source_file
I have 2 params in log file: timestamp, ip. What is the best way
params = urllib.urlencode(data) headers = {Content-type: application/x-www-form-urlencoded, Accept: text/plain, } conn = httplib.HTTPConnection(self.base_url) conn.request('GET',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.