Genie Discord forum

Author Avatarenergybill
7/18/2023, 7:53:26 PM

I am developing a Genie MVC app, and I'm trying to upload a .json file with a form and convert the file to a dictionary for use in a controller method.

The following is my HTML form to upload the file with the name "input":

 <form method="POST" action="/runs" enctype="multipart/form-data">
   <div class="input-group mb-3">
     <input type="file" id="files" class="form-control" accept=".json" name="input"/><br/>
     <input type="submit" class="btn btn-primary" value="Run REopt"/>
   </div>
 </form>

This routes to the /runs route: route("/runs", RunsController.create, method = POST)

Which calls the RunsController create() method:

function create()
    input = filespayload("input")
    input_dict = JSON.parse(input)
    run_inst = Run(input)
    if save(run_inst)
      redirect("/?success=Valid .json file successfully uploaded")
    else
      redirect("/?error=Could not save input&input=$(filespayload("input"))")
    end
end

The filespayload("input") is a Genie.Input.HttpFile, and I don't know how to parse this into a json or dictionary (the code above errors on input_dict = JSON.parse(input) because JSON cannot parse the HttpFile. Can you point me in the right direction? Thanks!

Author Avatarenergybill
7/21/2023, 9:11:24 PM

I figured it out: dict = JSON.parse(IOBuffer(filespayload("input").data))