About Rest API uploads (again)

126 views
Skip to first unread message

marco mansilla

unread,
Feb 4, 2018, 11:16:04 PM2/4/18
to web2py-users
Hi everyone, this is something that has been asked several times and still there's no specific solution.

I need to upload files by using a Rest API, so far I know we have nice ways to implement our apps in web2py

http://www.web2pyslices.com/slice/show/1533/restful-api-with-web2py is one way, simple and clean.

the other one is

http://experts4solutions.com/collection2/default/examples

both work perfectly fine wiith regular form data, as it comes with uploads they dont.

following the recipe from Bruno I can upload files by using curl like this

    curl POST -F "name=myfile" -F "image=@/home/user/myimgfile.png" http://127.0.0.1:8000/myapp/services/api/uploadimg

and it works.

In this new project I need to upload several images and some other format files (csv, doc, pdf, xls), as now I'm working with Vue.js and Axios it would be awesome (and somehow faster) applying it.

when I try to send the request using axios in this way

   let img=e.target.files;
   let formData = new FormData();

   formData.append('file', img[0]);
   axios.post('../services/api/uploadimg',{
        name:'test from axios',
        image:formData
        }, { headers: { 'Content-Type': 'multipart/form-data' } })
        .then((response)=>{console.log('file uploaded');})
        .catch((error)=>{console.log(error);});
    }

server returns an error

Traceback (most recent call last):
File "/home/user/web2py/gluon/restricted.py", line 219, in restricted
exec(ccode, environment)
File "/home/user/web2py/applications/myapp/controllers/services.py", line 50, in <module>
File "/home/user/web2py/gluon/globals.py", line 414, in <lambda>
self._caller = lambda f: f()
File "/home/user/web2py/gluon/globals.py", line 377, in f
res = rest_action(*request.args, **request.vars)
File "/home/user/web2py/gluon/globals.py", line 309, in vars
self.parse_all_vars()
File "/home/user/web2py/gluon/globals.py", line 280, in parse_all_vars
for key, value in iteritems(self.post_vars):
File "/home/user/web2py/gluon/globals.py", line 301, in post_vars
self.parse_post_vars()
File "/home/user/web2py/gluon/globals.py", line 237, in parse_post_vars
dpost = cgi.FieldStorage(fp=body, environ=env, keep_blank_values=1)
File "/usr/lib/python2.7/cgi.py", line 507, in __init__
self.read_multi(environ, keep_blank_values, strict_parsing)
File "/usr/lib/python2.7/cgi.py", line 621, in read_multi
raise ValueError, 'Invalid boundary in multipart form: %r' % (ib,)
ValueError: Invalid boundary in multipart form: '


couln't find much on that error but an old post in this group from 2012

https://groups.google.com/forum/#!topic/web2py/ixeUUWryZh0/discussion

Any suggestions?, meanwhile I'm still trying to make it work.

Greets.

Massimo Di Pierro

unread,
Feb 10, 2018, 11:43:43 PM2/10/18
to web2py-users
what's cgi.py? What web server are you using?

Marco Mansilla

unread,
Feb 10, 2018, 11:47:04 PM2/10/18
to web...@googlegroups.com
Firefox, and same error pops on Chrome, I'm using the default development server included un web2py. 

For now I worked around this by storing images as blobs. 

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marco Mansilla

unread,
Feb 10, 2018, 11:49:51 PM2/10/18
to web...@googlegroups.com
I'll try on apache and Nginx asap.

Val K

unread,
Feb 16, 2018, 8:36:47 PM2/16/18
to web2py-users
Are you trying  to insert formData in something another? I believe, that  formData should be passed to axios as is:
 axios.post('../services/api/uploadimg', formData)

if you need to add some fields just do it:
 formData.append('name', 'blah')


Marco Mansilla

unread,
Feb 17, 2018, 4:09:46 PM2/17/18
to web...@googlegroups.com
Yes, got it. I hace something like:


var image = event.target[0];
formData.append('name', image.name);
formData.append('myfile', image);
 axios.post('../services/api/uploads', formData).then(...).catch(...);

Params are correctly passed to the server, but files are not uploaded.

--

Val K

unread,
Feb 18, 2018, 6:41:36 AM2/18/18
to web2py-users
 it should be:
var image = event.target.files[0];  //not just `event.target[0]`;
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.

Marco Mansilla

unread,
Feb 18, 2018, 8:36:48 PM2/18/18
to web...@googlegroups.com
My bad, was a typho, still does't work.

In another thread read something related to file submission was a functionality of SQLFORM, and I'm using forms with Vue components.

As I said before, params inside formData are correct, and everything is processed ok by the server except files.

To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscribe@googlegroups.com.

Marco Mansilla

unread,
Feb 19, 2018, 2:37:34 AM2/19/18
to web...@googlegroups.com
Ok, so this is working right now, and most of errors were mine.

1. The error about cgi.py was related to wrong headers in axios request, and the need to use backticks to use correctly parameters in the url, so NOT web2py related

2.  Missed some details on FormData() docs, and did things wrong.

I'll try to make a small guide and simple sample app to show how easy is to make it work, actually  Web2py, VueJS and Axios play along really well.

Sorry to bother everyone with this.
--
-----BEGIN GEEK CODE BLOCK-----
version 3.1
GCS/GTW/| d? s+:+>+++ a-- C+++(+++++) UL++++>$ P++>+++ L+++>+$ E->+ W++>+ N>+++ o K- w---(++++)
O(--) M>-- V-- PS++ PE++>+ Y-->+ PGP+>+++ t-(?) 5? X++ R+ !tv b+++>++++ DI+++ D--- G+++>+++++
e+++>++++ h* r++>+++ y++
------END GEEK CODE BLOCK------
Reply all
Reply to author
Forward
0 new messages