How to extract data from multipart/mixed request in Spray

If you are getting multipart/mixed response then how should we handle that in spray so that we can extract all the data from that.

Suppose we are getting email request in which contains all the information such as: from , to, attachment etc.

So here is the way to extract those :

entity(as[MultipartContent]) { emailData =>
          post {
            val dataInParts = map {
              emailPart => ( -> emailPart)
            val dataInPartsMap = Map(dataInParts: _*)
	        val from= dataInPartsMap("From")
            val totalAttachment = dataInPartsMap("attachment-count")
            // If you want to find attachment details. 
  	        for (i <- 1 to totalAttachment) {
                   val fileType = dataInPartsMap("attachment-" + i).headers(1).value
                   val fileSize = dataInPartsMap("attachment-" + i).headers(0).value.toLong
                   val fileName = dataInPartsMap("attachment-" + i).filename.get

About Rishi Khandelwal

Sr. Software Engineer having around 4 years industry experience. He has working experience in various technologies such as Scala, Java, Play, Akka, Lift Web, Backbone.js, html5, javascript, Less, Amazon EC2, WebRTC, SBT
This entry was posted in Agile, Akka, Scala and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s