How to Change Content-Type Python
Question: I want to upload a file to a remote device. If i look up the connection with
wireshark i get this

    
    
    POST /saveRestore.htm.cgi HTTP/1.1
    Host: 10.128.115.214
    User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:15.0) Gecko/20100101 Firefox/15.0.1
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
    Accept-Encoding: gzip, deflate
    Connection: keep-alive
    Referer: http://10.128.115.214/saveRestore.htm
    Cache-Control: max-age=0
    Content-Type: multipart/form-data; boundary=---------------------------961265085509552220604142744
    Content-Length: 10708
    
    -----------------------------961265085509552220604142744
    Content-Disposition: form-data; name="restore"; filename="config(2).cfg"
    Content-Type: application/octet-stream
    

Now this says that that the browser only accepts
text/html,application/xhtml+xml,application/xml;q=0.9,_/_ ;q=0.8

If i upload the file with my script it says

    
    
    --0a7125aebb8845ba8ab9aa21306b01f6
    Content-Disposition: form-data; name="restore"; filename="Config.cfg"
    Content-Type: text/plain; charset=utf-8
    

So it's a wrong file type..

so how do i change the content-type of the File ?

My code looks so far as follows:

    
    
    #!/usr/bin/python
    
    import httplib
    import urllib2
    from poster.encode import multipart_encode
    import poster
    from poster.streaminghttp import register_openers
    register_openers()
    
    params = {'restore': open("Config.cfg", "rb"), 'upload': 'PC ==>; Unit'}
    
    datagen, headers = multipart_encode(params)
    
    request = urllib2.Request('http://10.128.115.214/saveRestore.htm.cgi', datagen, headers)
    u = urllib2.urlopen(request)
    print u.read()
    


Answer: In the [documentation for
`poster.encode.MultipartParam`](http://atlee.ca/software/poster/poster.encode.html#poster.encode.MultipartParam)
it says:

> If `filetype` is set, it is used as the Content-Type for this parameter. If
> unset it defaults to “text/plain; charset=utf8”

So instead of specifying your parameters like this:

    
    
    params = {'restore': open("Config.cfg", "rb"), 'upload': 'PC ==>; Unit'}
    

specify them like this:

    
    
    params = [MultipartParam('restore', open("Config.cfg", "rb"),
                             filetype = 'application/octet-stream'),
              ('upload', 'PC ==>; Unit')]
    

