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()
terminalcurl -X POST --user admin:password -d '{"Name": ["Alex", "Ronald"], "Age": [10, 18] }' -i
http://localhost:8000/test/api/csvresult on terminalHTTP/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 pathwith 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/csvgot the same result
objectionREST API method POST automatic download csv with the data content from curl -X POST
questionhow to do it in web2py way ?
thanks and best regards,
stifan