'How to specify file name for a download done via POST in akka http

The user sends a post request, than based on that post body I create an Excel file (.xlsx) and want to send that file back, without storage of that file itself.

def writeAsync(out: OutputStream): Unit = {
  Future {
    val wb = new XSSFWorkbook
    val sheet1: Sheet = wb.createSheet("sheet1");
    val os = new ByteArrayOutputStream()
    wb.write(os)
    os.writeTo(out)
    out.close()
    wb.close()
  }
}

...

pathPrefix("createAndDownloadExcel") {
  post {
    ...
    val generatedFileName = "customGeneratedFileName.xlsx" // <-- this name should the file name be like
    val (out, source) = StreamConverters.asOutputStream().preMaterialize()
    writeAsync(out)
    complete(HttpEntity(ContentTypes.`application/octet-stream`, source))
  }
}

The response has the excel content with the file name: "createAndDownloadExcel", but I would like it to have the file name based on the individual generated file name.

The name will be later manually generated based on the POST body, whereby a simple change in pathPrefix("fixedName.xlsx") does not work for my needs.

How can I solve this, being able to give a dynamic file name for that returned OutputStream?


"org.apache.poi" % "poi-ooxml" % "5.2.0" 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source