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

后端 未结 7 1738
慢半拍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:32

    Hmm I guess I should really start looking at the imports!

    I accidentally used import play.api.libs.ws.WS instead of import play.libs.WS; When using play.libs.WS all the methods such as post(String string) and setContentType(String string) revealed themselves. This is how I did it:

    import play.Play;
    import play.libs.F;
    import play.libs.WS;
    
    public static Result wsAction() {
        return async(
            play.libs.WS.url(Play.application().configuration()
                .getString("sms.service.url"))
                .setContentType("application/x-www-form-urlencoded; charset=utf-8")                       
                .post("param1=foo¶m2=bar").map(
                    new F.Function() {
                        public Result apply(WS.Response response) {
                           return ok(response.toString());
                        }
                    }
                )
            );
        }
    

提交回复
热议问题