How to handle multipart and non multipart request together in Spray

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 Lead Consultant, with experience of more than 7 years. Rishi is product focused developer who loves developing both front-end user interfaces and scalable back-end infrastructure. He is a good team player, quick learner and a humble person. He has good time management skills, aimed to give best results and fully dedicated towards his work & responsibilities. He is able to work as individual and as well as in team. He loves to share his knowledge, therefore he often writes technical blogs.

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!