How to upload csv file using Http POST

I have a simple csv file that I want to upload to my app using an HTTP endpoint

image

local code for uploading

import requests
files = {'upload_file': open("testfile33.csv",'rb')}
url='https://ak-api.anvil.app/_/api/upload'
response = requests.post(url, files=files)

I am unable to get the anvil server code (below) for http endpoint working properly to extract the file from the request. Here is what I have so far.

Greatly appreciate any help…

import anvil.server

@anvil.server.http_endpoint("/upload", methods="POST") 
def upload():
  bb = anvil.server.request.body.get_bytes()
  print(bb)

print result
b'--f0dbf62bee43b81ba5c7a76fb58af925\r\nContent-Disposition: form-data; name="upload_file"; filename="testfile33.csv"\r\n\r\nID,NAME\n1, "Jun"\n2, "Zune"\n3, "Boone"\r\n--f0dbf62bee43b81ba5c7a76fb58af925--\r\n'

Additional questions…

  1. I see the data in the message body, but how to grab it?
  2. Is this the method for large files say a 50 MB file?

Just poor understanding of requests on my part…I was passing the file stream object ( f ) instead of the file contents ( f.read() )

client side

url='https://ak-api-test.anvil.app/_/api/upload'
with open("testfile33.csv", 'r') as f:
  value=f.read()

response = requests.post(url, data=value)

server side

@anvil.server.http_endpoint("/upload", methods="POST") 
def upload(): 
  body = anvil.server.request.body
  body_str=body.get_bytes()
4 Likes