开发者

pycurl PostFields option usage

I'm trying to use pycurl to upload a file to Processmaker. app, self.usr, and doc are strings. file is a django file field object. I'm currently just passing the object. I'm fairly sure I'm just passing the incorrect object/type/thing to the ATTACH_FILE field.

The working php POSTFIELDS definition looks like this:

$params = array (
'ATTACH_FILE'  => '@/home/test.txt',
'APPLICATION'  => $resultCase->caseId,
'INDEX'        => 1,
'USR_UID'      => $oRandomUser->guid,
'DOC_UID'      => '3154812864d55a6e017f开发者_开发问答f65089604572',
'APP_DOC_TYPE' => 'INPUT',
'TITLE'        => "Initial document".date("Y-m-d H:i:s"),
'COMMENT'      => "this document was uploaded by the system"

curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

And the currently broken python:

    c = pycurl.Curl()
    data = [
            ('ATTACH_FILE', (pycurl.READFUNCTION, file.read)),
            ('APPLICATION', app),  
            ('INDEX' , 1),
            ('USR_UID', self.usr),
            ('DOC_UID', doc),
            ('APP_DOC_TYPE', 'INPUT')
           ]

    post = urllib.urlencode(data)

    print post

    url = "http://192.168.51.155/sysworkflow/en/green/services/upload"

    c.setopt(pycurl.URL, url)
    c.setopt(pycurl.VERBOSE, 1)
    c.setopt(pycurl.POST, 1)
    c.setopt(pycurl.POSTFIELDS, post)

    c.perform()

    c.close()

Any ideas?


I found a way to solve my own issue. Here is what I did, using poster located here: http://atlee.ca/software/poster/ I did the following:

from poster.streaminghttp import register_openers
import poster

register_openers()

url = "http://192.168.51.155/sysworkflow/en/green/services/upload"

params = { 
'APPLICATION' : app,  
'INDEX' : 1,
'USR_UID' : self.usr,
'DOC_UID' : doc,
'APP_DOC_TYPE' : 'INPUT',
'TITLE' : 'Test',
'ATTACH_FILE' : open(file.path, "rb")
}

datagen, headers = poster.encode.multipart_encode(params)
request = urllib2.Request(url, datagen, headers)
result = urllib2.urlopen(request)
print result.read()

Much easier to use than pycurl! The problem with my first attempt was that POSTFIELDS can't accept files (without some wrangling) and using an HTTPPOST option would work with the files but was difficult to get working with both file data and field data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜