How to handle multipart and non multipart request together in Spray

Table of contents
Reading Time: 2 minutes

In this blog, I will explain that how can we handle multipart and non multipart request together.

It is not necessary that we will get only one type of request either multipart or non multipart all the time. We can get either both of two. So we have to handle both type of request.

If we will handle only one type of request then if another type of request will come then we will not be able to handle that and nothing will happen.

So here, I am explaining the way to handle both type of request in spray.

We are getting the request on this URL:
http://"server IP":"Port"/email

In above, first one is handling multipart request while second one is handling non multipart (application url encoded) request.

Now if we get multipart request then we should handle this request like this :

Below is the method which extract attachment detail :

Below method is used for generating UID for attachments name. because we can get same name attachment more than once. So to differentiate, we prepend UID to each attachment file name.

And, if we get non multipart request (application-url-encoded) then we should handle like this :

Written by 

Rishi is a tech enthusiast with having around 10 years of experience who loves to solve complex problems with pure quality. He is a functional programmer and loves to learn new trending technologies. His leadership skill is well prooven and has delivered multiple distributed applications with high scalability and availability by keeping the Reactive principles in mind. He is well versed with Scala, Akka, Akka HTTP, Akka Streams, Java8, Reactive principles, Microservice architecture, Async programming, functional programming, distributed systems, AWS, docker.