John Doe John Doe - 1 year ago 100
Scala Question

Send POST request with parameters in body via Play WS API

Currently, I am sending get requests via the Play WS API as follows:

.withQueryString(getParams(): _*)

Now I want to change this call to use HTTP Post. When calling the following:

.withBody(getParams(): _*)

I receive the following error:

Cannot write an instance of Seq[(String, String)] to HTTP response. Try to define a Writeable[Seq[(String, String)]]

I guess it's because the method
Seq[(String, String)]

How can I fix this?

Answer Source

When using http post key value pairs are sent using content type application/x-www-form-urlencoded

Here is the code for posting

    .withHeaders("Content-type" -> "application/x-www-form-urlencoded")
    .post( { case (k, v) => s"$k=$v"}.mkString("&"))