How to send key-value pairs in body of a post request?

181 views
Skip to first unread message

missingfaktor

unread,
May 14, 2012, 2:46:24 PM5/14/12
to Scalatra
Hi, newbie here.

This is inside my Scalatra servlet:

  post("/register") {
    loggingExceptionOn("register") {
      val (name, password) = ("userName", "password").napply(params)
      logger.debug("Registering user %s.".format(name))
      transaction {
        User.register(name, password)
      }
      logger.debug("Registration successful.")
    }
  }

And this is from my test:

      val credentials = Map("userName" -> "natsu", "password" -> "dragneel")
      submit(method = "post", uri = "/snark/register", headers = credentials) {
        status must_== 200
      }

I want to send the name-value pairs in a post request so that they are available to servlet via params. However I am unable to understand how to pass them from test. The above doesn't work.

I can't send them in URL as they also contain a password.

Please help. 

--
Cheers,

Stefan Ollinger

unread,
May 14, 2012, 6:38:06 PM5/14/12
to scalat...@googlegroups.com
Hi,

i think you can use:

post("/snark/register", "userName" -> "natsu", "password" -> "dragneel") { .. }
submit(
method = "post", uri = "/snark/register", body = "userName=foo&password=bar") { .. }

Take a look at: https://github.com/scalatra/scalatra/blob/develop/test/src/main/scala/org/scalatra/test/Client.scala
That file defines the test helper methods.

Regards,
Stefan

Ross A. Baker

unread,
May 15, 2012, 1:36:04 AM5/15/12
to scalat...@googlegroups.com
Calling `get`, `post`, etc. is much more common than submit. The
params of `post`,`put`, and `patch` are submitted as a form body, and
the params of the other methods are submitted on the query string.

`submit` allows finer control if you don't like the way the other
methods are defined, or if you're using a non-standard HTTP method.
--
Ross A. Baker
ba...@alumni.indiana.edu
Indianapolis, IN, USA

missingfaktor

unread,
May 15, 2012, 1:38:53 AM5/15/12
to scalat...@googlegroups.com
Thanks for the pointer, Stefan. Using queryParams instead of headers helped.
--
Cheers,

missingfaktor

unread,
May 15, 2012, 1:44:38 AM5/15/12
to scalat...@googlegroups.com
Thanks for the tip, Ross. 
--
Cheers,

Reply all
Reply to author
Forward
0 new messages