Ryoichiro Oka
Ryoichiro Oka

Reputation: 1997

Posting application/x-www-form-urlencoded using spray

I want to "submit a form" through spray like this:

POST / HTTP/1.1
Host: hoge.org
Content-Type: application/x-www-form-urlencoded

username=myid&password=mypw

I know how to define POST, Host and Content-Type.
The thing is how to put the contents (username=...) in the request.

Here is my code waiting for the insertion of the contents:

//↑boiler plate↑
val pipe = (
  addHeader(Host("hoge.org"))
  ~> addHeader(`Content-Type`(`application/x-www-form-urlencoded`))
  ~> sendReceive.apply
)
pipe(Post("/")) onComplete {
  case Success(res) => println("okpk")
  case Failure(exc) => println(exc)
}

Thank you!

Upvotes: 1

Views: 1262

Answers (1)

Vladimir
Vladimir

Reputation: 499

You may use FormData marshaller:

pipe(Post("/", FormData(Seq(
      "username" -> "myid",
      "password" -> "mypw"))
    )) onComplete {
  case Success(res) => println("okpk")
  case Failure(exc) => println(exc)
}

Upvotes: 3

Related Questions