In spray how to unmarshall plain/text?
Asked Answered
T

1

2

Can anybody show me an example in spray how to unmarshall POST with content type plain/text ?

How to write the route?

Thanks!

Thornberry answered 22/11, 2014 at 1:49 Comment(0)
D
2

This is how the route would look like:

  post {
    entity(as[String]) { str =>
      complete(str)
    }
  }

A sample request would be:

curl -X POST -d 'It works' localhost:8080

which echoes back the POST data.

To get the whole sample app use this official template and add/edit the route.

If you want to understand how it works in detail see this doc and this one.

Discophile answered 22/11, 2014 at 8:54 Comment(5)
Thanks for your answer, I tried this piece of code together with github.com/mhamrah/spray-sample, and I got the message "Expected 'application/json'", it seems that some setting need be done. Can you kindly explain to me? Thanks so much!Thornberry
HTTP/1.1 415 Unsupported Media Type * Server spray-can/1.3.1 is not blacklisted < Server: spray-can/1.3.1 < Date: Sat, 22 Nov 2014 21:07:51 GMT < Content-Type: text/plain; charset=UTF-8 < Content-Length: 79 < There was a problem with the requests Content-Type: * Connection #0 to host localhost left intact Expected 'application/json'Thornberry
please try this in a service without JSON support, i.e. not in that project. It seems that JSON unmarshaller takes over all conversions and sets the expected media type to json. I'll try to give a better explanation soon when I find some time ;). Here is a good template to start with: github.com/spray/spray-templateLodestone
Yes, you need to be careful with importing SprayJsonSupport._ etc. Optimally, you just import it exactly in the scopes where you need it and not globally for the file which contains all of your routes.Adjutant
Any idea why in akka http string implicit is not always available? see groups.google.com/forum/#!topic/akka-user/p-m1eKm3-YMSeclude

© 2022 - 2024 — McMap. All rights reserved.