2

I am using Spray-can to host a REST service to which a user will be able to upload a file. The block of code that listens for incoming requests is given below:

def receive: Receive = {
case _: Http.Connected => sender ! Http.Register(self)

case req@HttpRequest(HttpMethods.POST, Uri.Path("/upload"), headers, entity, _) =>
  logger.info("Received file upload request.")

// Process the uploaded data using the 'entity' object

I upload the file using this curl command:

curl --data-binary @inputFile.csv 'devserver:8998/upload?tenant=DressShop&facility=CityCenter&customer=Jimmy'

The challenge I am facing is that I'm not able to pick up the filename "inputFile.csv" from the request, though I'm getting the data from the "entity" object. I tried poring through the API but couldn't find out any way to get the filename.

My objective is to ensure that I allow upload of only csv files.

James Isaac
  • 2,587
  • 6
  • 20
  • 30

2 Answers2

1

You need to process the entity as form data using as

as[MultipartFormData]

Then you can get the file name from the header fields:

    def processFormData(data: MultipartFormData) = {
        var attForm = ""
        val bodyPart = data.fields(0)

        data.fields foreach {
            bodyPart => println(bodyPart.headers.find(h=> h.is("content-disposition")).get.value)
        }

    }
Max Heiber
  • 14,346
  • 12
  • 59
  • 97
0

This might help:

enter image description here

The filename can be found in parameters.

jhegedus
  • 20,244
  • 16
  • 99
  • 167