Re: [issue2551067] Document file amd msg upload for rest interface

Ralf Schlatterbeck <[email protected]>
Newsgroups gmane.comp.bug-tracking.roundup.devel
Message-ID <[email protected]>
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: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.