curl-and-python

Re: Setting content type for HTTPPOST?

From: Michael Wood <esiotrot_at_gmail.com>
Date: Tue, 12 Oct 2010 08:25:24 +0200

On 11 October 2010 20:16, <johansen_at_opensolaris.org> wrote:
> On Mon, Oct 11, 2010 at 07:02:36PM +0200, Michael Wood wrote:
>> Hi
>>
>> I'm using PycURL to upload a file but I'm having trouble figuring out
>> how to specify the content type of the file.  It's currently coming
>> through as text/plain and the server seems to be converting the DOS
>> line end characters to UNIX line end characters.  When the file is
>> uploaded, the server calculates the checksum to send back to the
>> client so that the client knows that the file is not corrupted, so
>> this EOL conversion breaks the checksum.
>>
>> At the moment I'm doing this:
>>
>>         pf = [("report", (pycurl.FORM_FILE, filename)),
>>               ("checksum", self.checksum)]
>>         curl.setopt(pycurl.HTTPPOST, pf)
>>
>> I see there's a FORM_CONTENTTYPE option, but I haven't noticed
>> documentation on it or exactly how to use it.  Perhaps I've been
>> looking in the wrong place, in which case I'd appreciate a pointer to
>> the right place :)
>
> At this point, the documentation is the code itself.  When in doubt, I
> look there first.  The HTTPPOST code handles the arguments as FORMTYPE,
> argument pairs.  This looks a lot like the interface used here:
>
> http://curl.haxx.se/libcurl/c/curl_formadd.html
>
> I believe that the following should work for you:
>
>        pf = [(pycurl.FORM_FILE, filename, pycurl.FORM_CONTENTTYPE,
>        content_type)]
>        curl.setopt(pycurl.HTTPPOST, pf)
>
> HTH,

Thanks, I tried that, but there was no change.

I've since found tests/test_post2.py, but unfortunately it doesn't
test the FORM_CONTENTTYPE option. It contains this confusing part,
though:

pf = [('field1', 'this is a test using httppost & stuff'),
      ('field2', (pycurl.FORM_FILE, 'test_post.py', pycurl.FORM_FILE,
'test_post2.py')),
      ('field3', (pycurl.FORM_CONTENTS, 'this is wei\000rd, but
null-bytes are okay'))
     ]

Is it posting two different files to field2?

I'll play around some more, but if anyone has any more suggestions,
please let me know :)

-- 
Michael Wood <esiotrot_at_gmail.com>
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-python
Received on 2010-10-12