hello,

*test/api.py*def csv():
    import pandas as pd
    from io import StringIO

    import datetime

    today = datetime.date.today()

    req_vars = request.vars

    if len(req_vars) == 1 and '' in req_vars.values():
        vars0 = "%s" % ",".join(["%s:%s" % (k,v) for k,v in 
req_vars.items() ] )
        vars1 = vars0[:-1]

        import ast
        vars2 = ast.literal_eval(vars1)

        import json
        vars3 = json.loads(vars1)
    else:
        vars3 = req_vars

    stream = StringIO()

    filename = 'Test - %s' % (today)

    df = pd.DataFrame(vars3)

    df.to_csv(stream, index = False, header = True, encoding = 'utf-8', sep 
= ',')

    response.headers['Content-Type'] = 'text/csv'
    response.headers['Content-Disposition'] = 'attachment; 
filename="{0}.csv"'.format(filename)

    return stream.getvalue()

*terminal*
curl -X POST --user admin:password -d '{"Name": ["Alex", "Ronald"], "Age": 
[10, 18] }' -i http://localhost:8000/test/api/csv

*result on terminal*
HTTP/1.1 200 OK
X-Powered-By: web2py
Content-Type: text/csv
Content-Disposition: attachment; filename="Test - 2022-02-12.csv"
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, 
pre-check=0
Expires: Sat, 12 Feb 2022 07:26:56 GMT
Pragma: no-cache
Date: Sat, 12 Feb 2022 07:26:56 GMT
Server: Rocket 1.2.6 Python/3.8.12
Content-Length: 27
Connection: keep-alive

Name,Age
Alex,10
Ronald,18

*result on system path*
with no file downloaded on system path
already tried
curl *-o output.csv* -X POST --user admin:password -d '{"Name": ["Alex", 
"Ronald"], "Age": [10, 18] }' -i http://localhost:8000/test/api/csv

got the same result

*objection*
REST API method POST automatic download csv with the data content from curl 
-X POST

*question*
how to do it in web2py way ?

thanks and best regards,
stifan

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/6f8e9ea0-6106-4d39-bd51-d429950ab2b3n%40googlegroups.com.

Reply via email to