How do I set params for WS.post() in play 2.1 Java

后端 未结 7 1737
慢半拍i
慢半拍i 2020-12-09 20:43

I\'m trying to perform a post with play.api.libs.ws.WS but I can\'t figure out how to set the params, my code:

Promise promise = WS.url(Play.         


        
7条回答
  •  执笔经年
    2020-12-09 21:30

    The accepted answer is wrong, or at least misleading. The code

    WS.url("http://localhost:9001")
        .setQueryParameter("param1", "foo")
        .setQueryParameter("param2", "bar")
        .post("content");
    

    will post the string content to http://localhost:9001/?param1=foo¶m2=bar, which is almost certainly not what the OP wanted. What is much more likely to work is

    WS.url("http://localhost:9001")
       .post(Map("param1" -> Seq("foo"),
                 "param2" -> Seq("bar")))
    

    which posts the form param1=foo¶m2=bar to the the URL http://localhost:9001, which is typically what the server wants.

提交回复
热议问题