使用spray/scala从发布请求中获取表单参数
我对 Scala/Spray 真的很陌生。通过一些测试,我能够使用参数函数从 Get 请求中获取参数。不过,我试图在请求正文中获取从 POST 请求发送的一些参数。参数函数似乎无法获取这些值。
例如,我试图从发布请求正文中获取此值“name=john&lastname=smith”。获得这些值的最佳选择是什么?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 [Form-Field-Filters] 从 POST 中提取参数
[Form-Field-Filters] https://github.com/spray/spray/wiki/Form-Field-Filters
You could use [Form-Field-Filters] to extract parameters from POSTs
[Form-Field-Filters] https://github.com/spray/spray/wiki/Form-Field-Filters
事实上,
parameters
指令仅处理实际的事情请求正文中的查询字符串而不是参数。要将内容从主体中取出,您需要使用content
指令,然后使用 解组内容。此喷雾-用户线程可能会有所帮助,因为它包括一些解组代码正是您正在寻找的。
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 thecontent
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.
从最新的 Spray 版本开始,您需要对
FormData
使用Unmarshaller
。As of recent Spray versions, you need to use the
Unmarshaller
forFormData
.