Akka HTTP POST accepted media types

1,245 views
Skip to first unread message

Christian Pérez-Llamas

unread,
Mar 18, 2016, 7:58:14 AM3/18/16
to Akka User List
Hi, I am doing my first steps with Akka HTTP with no previous experience with Spray. I need to implement a POST that can accept either text/plain or application/json and returns application/json. I am using akka-http-json library for marshalling/unmarshalling json.

This is the part of the route that deals with it:

path("things") {
post {
decodeRequest {
request.entity.contentType.mediaType match {
case MediaTypes.`text/plain` =>
import PredefinedFromEntityUnmarshallers.stringUnmarshaller
entity(as[String]) { body => // Every line can contain things, empty lines or comments
complete {
Source.fromString(body).getLines
.map(_.trim)
.filterNot(line => line.isEmpty || line.startsWith("#"))
.toSeq
}
}
case MediaTypes.`application/json` =>
entity(as[List[Thing]]) { things =>
complete(things.map(_.name))
}
case _ =>
reject
}
}
}
}

When I do a request with curl with 'Content-Type:text/csv' for example I get a:

HTTP/1.1 404 Not Found
The requested resource could not be found.

My gut intuition tells me that I am not doing the media type multiplexing the right way. What is the idiomatic way to handle different media types ?, and how can I specify the allowed ContentTypes (or media types) so the response error is more specific ?

Thank you very much,
Christian

Konrad Malawski

unread,
Mar 18, 2016, 8:03:27 AM3/18/16
to akka...@googlegroups.com, Christian Pérez-Llamas
Hi, there's some things that can be improved here (I'll follow up later),
however the first thing that confused me:

You mention (and handle) text/plain everywhere, yet your CURL example is about text/csv - 
which is different so I'd expect the 404 actually.

Is that a typo or the actual question?

-- 
Cheers,
Konrad 'ktoso’ Malawski
Akka @ Lightbend
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Christian Pérez-Llamas

unread,
Mar 18, 2016, 8:27:18 AM3/18/16
to Akka User List, chr...@gmail.com
Hi, sorry, I was asking too many things and not being clear.

I am doing a curl with an intentionally unsupported content-type to see the response in those cases. I was expecting something more specific such as:

HTTP/1.1 415 Unsupported Media Type
The request's Content-Type is not supported. Expected:
application/json
text/plain

but getting "404 The requested resource could not be found." made me think that the way I am handling multiple content types is not the right way with Akka Http.

Thank you so much,
Christian

Konrad Malawski

unread,
Mar 18, 2016, 8:29:30 AM3/18/16
to akka...@googlegroups.com, Christian Pérez-Llamas, chr...@gmail.com
Ah yes :-)
Agreed and that's because of what I hinted at "things can be improved, will follow up" in the first email :-)

Effectively the response you want is indeed produced by Akka, however only when content negotiation is left up to Akka,
whereas you basically did it manually (i.e. if doesnt match you manually reject – which results in "nothing matched... 404!").

I'll write up an example of properly doing that eventually, soon I hope, as I think it needs a proper docs page.
Thanks for your patience, will ping here :)

-- 
Cheers,
Konrad 'ktoso’ Malawski
Akka @ Lightbend

Christian Pérez-Llamas

unread,
Mar 18, 2016, 8:45:38 AM3/18/16
to Akka User List, chr...@gmail.com
Thanks Konrad, looking forward for your example.

Meanwhile I just discovered how to use reject() to be more specific, like:

reject(UnsupportedRequestContentTypeRejection(Set(
MediaTypes.`text/plain`, ContentTypes.`application/json`
)))
Reply all
Reply to author
Forward
0 new messages