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 = emailData.parts.seq map {
              emailPart => (emailPart.name.get -> emailPart)
            }
            val dataInPartsMap = Map(dataInParts: _*)
	        val from= dataInPartsMap("From").entity.data.asString
            val totalAttachment = dataInPartsMap("attachment-count").entity.data.asString.toInt
            // 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
            }
           complete("OK")
          }
        }

About Rishi Khandelwal

Software Consultant at Knoldus Software LLP, New Delhi, India having 3+ 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:

WordPress.com Logo

You are commenting using your WordPress.com 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