Get form parameters from a post request using spray/scala
Asked Answered
E

3

12

I'm really new with all this Scala/Spray. With some testing I was able to get parameters from a Get request using the parameters function. However I'm trying to fetch some parameters sent from a POST request on the body of the request. It seems like parameters function is unable to fetch those values.

As an example, I'm trying to get this values "name=john&lastname=smith" from the post request body. What is the best option to get these values?

Thank you

Exoenzyme answered 14/10, 2011 at 15:21 Comment(2)
would you be able to use Request.QueryString("name") ?Aluminothermy
This won't work if the parameter isn't in the query string.Agamogenesis
A
4

Indeed, the parameters directive only handles things actually in the query-string and not parameters in the body of the request. To get things out of the body, you'll need to use the content directive and then unmarshal the content.

This spray-user thread may be helpful, as it includes some unmarshalling code doing precisely what you're looking for.

Agamogenesis answered 15/10, 2011 at 16:48 Comment(1)
FYI, the content directive was recently renamed into entity. Also, as the other answer says, there's the formFields directive which allows to extract data from POST data.Perfecto
N
5

You could use [Form-Field-Filters] to extract parameters from POSTs

[Form-Field-Filters] https://github.com/spray/spray/wiki/Form-Field-Filters

Niobium answered 4/1, 2012 at 15:11 Comment(0)
A
4

Indeed, the parameters directive only handles things actually in the query-string and not parameters in the body of the request. To get things out of the body, you'll need to use the content directive and then unmarshal the content.

This spray-user thread may be helpful, as it includes some unmarshalling code doing precisely what you're looking for.

Agamogenesis answered 15/10, 2011 at 16:48 Comment(1)
FYI, the content directive was recently renamed into entity. Also, as the other answer says, there's the formFields directive which allows to extract data from POST data.Perfecto
L
0

As of recent Spray versions, you need to use the Unmarshaller for FormData.

Lozier answered 15/9, 2015 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.