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

  • Home
  • SEARCH
  • 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 8725761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:06:21+00:00 2026-06-13T08:06:21+00:00

How can I POST parameters with a file object to a URL using httplib

  • 0

How can I POST parameters with a file object to a URL using httplib in python web services.

Am using the following scripts:

import httplib
import urllib
params = urllib.urlencode({"@str1":"string1", "@str2":"string2", "@file":"/local/path/to/file/in/client/machine", "@action":"action.php" })
headers = {"Content-type":"application/pdf , text/*" }
conn = httplib.HTTPConnection("192.168.2.17")
conn.request("POST", "/SomeName/action.php", params, headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
data
conn.close()

And I have the following output:

200
OK
<html>.....some html code </html>

I wrote some php code for save those string and the file in DB
My problem is that, Am only getting the file path as a sting but not my file.
May be I have to send the file object like,

file_obj = open("filename.txt","r")
conn.request("POST", "/SomeName/action.php", file_obj, headers)

But I want to send both strings and file. Any suggestions to solve this?

EDIT
I change my code as follows:
When i send a pdf file, by directly using httplib, to my server the file saves as BIN file.

def document_management_service(self,str_loc_file_path,*args):
    locfile = open(str_loc_file_path,'rb').read()
    host = "some.hostname.com"
    selector = "/api/?task=create"
    fields = [('docName','INVOICE'),('docNumber','DOC/3'),('cusName','name'),('cusNumber','C124'),('category','INVOICE'),('data','TIJO MRS,SOME/DATA/CONTENT,Blahblah,2584.00,blahblah'),('action','create')]
    files = [('strfile','File.pdf',locfile)]
    response = self.post_multipart(host, selector, fields, files)
    print response
    pass

def post_multipart(self,host, selector, fields, files):
    content_type, body = self.encode_multipart_formdata(fields, files)
    h = httplib.HTTP(host)
    h.set_debuglevel(1)
    h.putrequest('POST', selector)
    h.putheader('content-type', content_type)
    h.putheader('content-length', str(len(body)))
    h.putheader('Host', host)
    h.endheaders()
    h.send(body)
    errcode, errmsg, headers= h.getreply()
    return h.file.read()

def encode_multipart_formdata(self, fields, files):
    LIMIT = '----------lImIt_of_THE_fIle_eW_$'
    CRLF = '\r\n'
    L = []
    for (key, value) in fields:
        L.append('--' + LIMIT)
        L.append('Content-Disposition: form-data; name="%s"' % key)
        L.append('')
        L.append(value)
    for (key, filename, value) in files:
        L.append('--' + LIMIT)
        L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
        L.append('Content-Type: %s' % self.get_content_type(filename))
        L.append('')
        L.append(value)
    L.append('--' + LIMIT + '--')
    L.append('')
    body = CRLF.join(L)
    content_type = 'multipart/form-data; boundary=%s' % LIMIT
    return content_type, body

def get_content_type(self, filename):
    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'

I have debug the request which shows as:

[('content-length', '4191'), ('accept-ranges', 'bytes'), ('server', 'Apache/2.2.12 (Ubuntu)'), ('last-modified', 'Tue, 23 Oct 2012 04:46:36 GMT'), ('etag', 'W/"567dd-105f-4ccb2a7a9a500"'), ('date', 'Tue, 23 Oct 2012 04:46:36 GMT'), ('content-type', 'application/pdf')]
multipart/form-data; boundary=----------lImIt_of_THE_fIle_eW_$

And I didn’t try requests,Coz I would like to solve this with httplib(without any external lib)

  • 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-13T08:06:23+00:00Added an answer on June 13, 2026 at 8:06 am

    To post parameters and a file in a body you could use multipart/form-data content type:

    #!/usr/bin/env python
    import requests # $ pip install requests
    
    file = 'file content as a file object or string'
    r = requests.post('http://example.com/SomeName/action.php',
                      files={'file': ('filename.txt', file)},
                      data={'str1': 'string1', 'str2': 'string2'})
    print(r.text) # response
    

    requests.post sends to the server something like-this:

    POST /SomeName/action.php HTTP/1.1
    Host: example.com
    Content-Length: 449
    Content-Type: multipart/form-data; boundary=f27f8ef67cac403aaaf433f83742bd64
    Accept-Encoding: identity, deflate, compress, gzip
    Accept: */*
    
    --f27f8ef67cac403aaaf433f83742bd64
    Content-Disposition: form-data; name="str2"
    Content-Type: text/plain
    
    string2
    --f27f8ef67cac403aaaf433f83742bd64
    Content-Disposition: form-data; name="str1"
    Content-Type: text/plain
    
    string1
    --f27f8ef67cac403aaaf433f83742bd64
    Content-Disposition: form-data; name="file"; filename="filename.txt"
    Content-Type: text/plain
    
    file content as a file object or string
    --f27f8ef67cac403aaaf433f83742bd64--
    

    To reproduce it with httplib see POST form-data with Python example.

    A simpler solution if your parameters do not contain much data is to pass them in the url query part and leave the body to contain only the file:

    #!/usr/bin/env python
    import urllib
    import requests # $ pip install requests
    
    params = {'str1': 'string1', 'str2': 'string2', 'filename': 'filename.txt'}
    file = 'file content as a file object or string, etc'    
    url = 'http://example.com/SomeName/action.php?' + urllib.urlencode(params)
    r = requests.post(url, data=file, headers={'Content-Type': 'text/plain'})
    print(r.text) # response
    

    It corresponds to the following HTTP request:

    POST /SomeName/action.php?str2=string2&str1=string1&filename=filename.txt HTTP/1.1
    Host: example.com
    Content-Length: 39
    Content-Type: text/plain
    Accept-Encoding: identity, deflate, compress, gzip
    Accept: */*
    
    file content as a file object or string
    

    It should be easier to translate to httplib if you need it.

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

Sidebar

Related Questions

How can I add parameters to a post request using .post or .ajax. I
I just want a function that can take 2 parameters: the URL to POST
I need to test an API of following spec URL: http://myapp.com/api/upload/ Method: POST Parameters:
How much some one can modify a javascript to intercept the post parameters to
I am trying to read the post request parameters from my HTML. I can
How can I parse the query string (GET / POST parameters) sent to an
plupload creates nice ids in file object . How this id can be sent
I have a file test.xml that I want to POST up to a URL
I can post on group / page and user wall, but i don't want
In Windows I can post custom message to another process and inform it to

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.