Roundup Tracker - Issues

Message6747

Author schlatterbeck
Recipients rouilj, schlatterbeck
Date 2019-10-15.13:13:39
Message-id <20191015131334.3h4qoopfsqiphydy@runtux.com>
In-reply-to <1571106141.96.0.315807198914.issue2551067@roundup.psfhosted.org>
On Tue, Oct 15, 2019 at 02:22:21AM +0000, John Rouillard wrote:
> 
> Files however are not the same. We support creation of the metadata
> using json, but I am not sure how we can post the metadata and then
> set the content separately.
> Would we use a multipart/form-data posted to /rest/data/file to
> include the metadata and content?

I've successfully written files (metadata and content property) using
the method from the request library. The idea is instead of specifying
json = dictionary we specify data = dictionary.

        d = dict (name = filename, content = content, type = content_type)
        j = self.post ('file', data = d)

The self.post method sets up the necessary headers and prefixes the
given path.

I *think* this by default sends the contents as
application/x-www-form-urlencoded
which is sub-optimal for large files.

You can force the requests library to use multipart/form-data by
specifying both, files= *and* data= parameters, e.g.,

# A binary string that can't be decoded as unicode
content = open ('random-junk', 'rb').read ()
fname   = 'a-bigger-testfile'
d = dict \
    ( name = fname
    , type='application/octet-stream'
    )
c = dict (content = content)
r = session.post (url + 'file', files = c, data = d)
print (r.json ())

This produces something like

POST /path/to/tracker/.../file HTTP/1.1
Host: bee:8080
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: python-requests/2.12.4
Content-Length: 2405
Content-Type: multipart/form-data;
boundary=788e954792774a6cbe747ba2ca2a276a
Authorization: Basic <censored>

--788e954792774a6cbe747ba2ca2a276a
Content-Disposition: form-data; name="type"

application/octet-stream
--788e954792774a6cbe747ba2ca2a276a
Content-Disposition: form-data; name="name"

a-bigger-testfile
--788e954792774a6cbe747ba2ca2a276a
Content-Disposition: form-data; name="content"; filename="content"

i.S...Em..3/].T...e1ag.G..?N.b.%..P`M..#a...r.S......}>..d.>7.3a...n.."..`
.P.[.aQc..Rg.....q...s1z.9........%..]..|..1.|...M..p.GC....=..BV.L.5..
+.F.!..H...gI..cdg?.........k...t..A..........}`...J.....
....Y.....>....{..E..
%.E...:a.o.F.......o...../..).>o..qmm.U7..BT..
--788e954792774a6cbe747ba2ca2a276a--

And, yes, this works for creating files :-)

Ralf
-- 
Dr. Ralf Schlatterbeck                  Tel:   +43/2243/26465-16
Open Source Consulting                  www:   http://www.runtux.com
Reichergasse 131, A-3411 Weidling       email: office@runtux.com
History
Date User Action Args
2019-10-15 13:13:40schlatterbecksetrecipients: + schlatterbeck, rouilj
2019-10-15 13:13:40schlatterbecklinkissue2551067 messages
2019-10-15 13:13:39schlatterbeckcreate