uploading a file with REST

37 views
Skip to first unread message

Ian Reinhart Geiser

unread,
Aug 16, 2009, 11:53:54 AM8/16/09
to web2py-users
Greetings, Currently I have a lovely web form that was fairly easy to
implement with web2py. I now also have cron script that is uploading
snapshots of an image from a webcam to my website. What I would like
to do is have a service url that I could post my image to and have it
automatically be added vs going through the form.

From what I can tell is I would have a run REST url and i would read
the request.body object to get the file. What I am unclear on is how
I might put this on the filesystem and the needed links in the
database correctly. Do I have to roll my own update code vs using the
DAL?

Thanks in advance!

mdipierro

unread,
Aug 16, 2009, 1:50:29 PM8/16/09
to web2py-users
if you have:

def mycreate(): return dict(form=crud.create(db.mytable))

you normally call: http://..../mycreate to get the form.
You can also call it with

http://..../mycreate.json?json=[]

where [] is a URLencoded JSONencoded dictionary containing the new
record. The ".json" instructs web2py not to generate the form but to
expect a json input. This is experimental but give it try. Should
work for any page including crud forms.

Massimo

mdipierro

unread,
Aug 16, 2009, 1:55:02 PM8/16/09
to web2py-users
Correction. This works but it probaby does not work with upload.

you may need something like

@auth.requires_login()
def uploadme(): db.mytable.insert(filefield=request.body.read())

On Aug 16, 12:50 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
> if you have:
>
> def mycreate(): return dict(form=crud.create(db.mytable))
>
> you normally call:http://..../mycreateto get the form.

Ian Reinhart Geiser

unread,
Aug 16, 2009, 10:56:16 PM8/16/09
to web2py-users
Right now im doing this but getting errors:
@service.run
def upload_image( camera_uuid ):
query = (db.cameras.macaddress == camera_uuid )
camera_id = db(query).select(db.cameras.id)[0]
db.videos[0]={ 'camera' : camera_id, 'file' : db.videos.file.store
(request.body, 'upload.flv') }
return True

The problem is that its generating 0 size files. But the file is
correct, and is created in the database correctly. Did i miss a step?

I am using the following curl line:
curl -T ~/test.flv http://web2py.geiseri.com/HomeCentral/default/call/run/upload_image/camera1

I feel like im close...

On Aug 16, 1:55 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
> Correction. This works but it probaby does not work with upload.
>
> you may need something like
>
> @auth.requires_login()
> def uploadme(): db.mytable.insert(filefield=request.body.read())
>
> On Aug 16, 12:50 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > if you have:
>
> > def mycreate(): return dict(form=crud.create(db.mytable))
>
> > you normally call:http://..../mycreatetoget the form.

mdipierro

unread,
Aug 17, 2009, 3:18:48 AM8/17/09
to web2py-users
This is the best way of doind it. Can you try add

print request.vars.keys
request.body.seek(0)

?

On Aug 16, 9:56 pm, Ian Reinhart Geiser <ian.gei...@gmail.com> wrote:
> Right now im doing this but getting errors:
> @service.run
> def upload_image( camera_uuid ):
>     query = (db.cameras.macaddress == camera_uuid )
>     camera_id = db(query).select(db.cameras.id)[0]
>     db.videos[0]={ 'camera' : camera_id, 'file' : db.videos.file.store
> (request.body, 'upload.flv') }
>     return True
>
> The problem is that its generating 0 size files.  But the file is
> correct, and is created in the database correctly.  Did i miss a step?
>
> I am using the following curl line:
> curl -T ~/test.flvhttp://web2py.geiseri.com/HomeCentral/default/call/run/upload_image/c...
>
> I feel like im close...
>
> On Aug 16, 1:55 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > Correction. This works but it probaby does not work with upload.
>
> > you may need something like
>
> > @auth.requires_login()
> > def uploadme(): db.mytable.insert(filefield=request.body.read())
>
> > On Aug 16, 12:50 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > > if you have:
>
> > > def mycreate(): return dict(form=crud.create(db.mytable))
>
> > > you normally call:http://..../mycreatetogetthe form.

Ian Reinhart Geiser

unread,
Aug 17, 2009, 8:25:41 AM8/17/09
to web2py-users
I see this when i return request.vars.keys() I get an empty array.
That seems a little off... doesn't it? Could it be that I am missing
a step with respects to reading the request object?

Thanks for your insight so far!

mdipierro

unread,
Aug 17, 2009, 8:31:11 AM8/17/09
to web2py-users
The problem is that I do not undestand where curl is putting the
image.
If it is not in request.vars.keys() then curl is not doing a multipart
form submission.
If the file is in request.body that it will not be parsed and stored
in request.vars but this:

request.body.seek(0)
db.videos.file.store(request.body, 'upload.flv')

should work.
Massimo

Ian Reinhart Geiser

unread,
Aug 19, 2009, 12:40:51 PM8/19/09
to web2py-users
Ah, this was the magic. the db.videos.file.store line. It seems to
work just fine now.
Reply all
Reply to author
Forward
0 new messages