[akka-user] Akka HTTP POST accepted media types

2016-03-19 Thread Christian Pérez-Llamas
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

-- 
>>  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+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


Re: [akka-user] Akka HTTP POST accepted media types

2016-03-19 Thread Konrad Malawski
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

On 18 March 2016 at 13:27:22, Christian Pérez-Llamas (chri...@gmail.com) wrote:

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

On Friday, 18 March 2016 12:03:27 UTC, Konrad Malawski wrote:
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

On 18 March 2016 at 12:58:11, Christian Pérez-Llamas (chr...@gmail.com) wrote:

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
--
>> 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.
--
>> 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+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

-- 
>>  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+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroup

Re: [akka-user] Akka HTTP POST accepted media types

2016-03-19 Thread Christian Pérez-Llamas
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

On Friday, 18 March 2016 12:03:27 UTC, Konrad Malawski wrote:
>
> 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 
> 
>
> On 18 March 2016 at 12:58:11, Christian Pérez-Llamas (chr...@gmail.com 
> ) wrote:
>
> 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
> --
> >> 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.
>
>

-- 
>>  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+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


Re: [akka-user] Akka HTTP POST accepted media types

2016-03-18 Thread Konrad Malawski
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

On 18 March 2016 at 12:58:11, Christian Pérez-Llamas (chri...@gmail.com) wrote:

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
--
>> 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+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

-- 
>>  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+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


Re: [akka-user] Akka HTTP POST accepted media types

2016-03-18 Thread Christian Pérez-Llamas
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`
)))



On Friday, 18 March 2016 12:29:30 UTC, Konrad Malawski wrote:
>
> 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 
> 
>
> On 18 March 2016 at 13:27:22, Christian Pérez-Llamas (chr...@gmail.com 
> ) wrote:
>
> 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
>
> On Friday, 18 March 2016 12:03:27 UTC, Konrad Malawski wrote: 
>>
>> 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 
>>  
>>
>> On 18 March 2016 at 12:58:11, Christian Pérez-Llamas (chr...@gmail.com) 
>> wrote:
>>
>> 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
>> --
>> >> 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.
>>
>> --
> >> 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 unsubscri