'Download excel file from POST REST request Scala

I have a REST API that returns excel file in bytes[] producing in excel ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") format. After calling that API from scala, I am getting bytes[], now I want to save the byte[] response in a temporary excel file. This excel file will later be used to create a data frame. Directly creating a dataframe from byte[] will also be useful. Please help me out.

 val result = Http("http://localhost:8080/orders/download")
  .postData("{\"orderNumbers\": [\"1\"]}")
  .header("Accept", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  .header("Content-Type", "application/json")
  .option(HttpOptions.readTimeout(10000)).asBytes.body


val dframe = sparkSession.read
  .format("com.crealytics.spark.excel")
  .option("header", "true")
  .option("inferSchema", "true")
  .load("report.xlsx"); // local path

edit : i tried this :

import java.io.File
val newFile = File.createTempFile("report1", ".xlsx")

val bos = new BufferedOutputStream(new FileOutputStream(newFile))
bos.write(result)
bos.close() 

val dframe = sparkSession.read
  .format("com.crealytics.spark.excel")
  .option("header", "true")
  .option("inferSchema", "true")
  .load(newFile.getPath); // local path

`

getting the below exception now:

java.io.IOException: Your InputStream was neither an OLE2 stream, nor an OOXML stream
at shadeio.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:234)
at shadeio.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:198)
at com.crealytics.spark.excel.DefaultWorkbookReader$$anonfun$openWorkbook$1.apply(WorkbookReader.scala:50)
at com.crealytics.spark.excel.DefaultWorkbookReader$$anonfun$openWorkbook$1.apply(WorkbookReader.scala:50)
at scala.Option.fold(Option.scala:158)
at com.crealytics.spark.excel.DefaultWorkbookReader.openWorkbook(WorkbookReader.scala:50)


Sources

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

Source: Stack Overflow

Solution Source