Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

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

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<Response> promise = WS.url(Play.application().configuration()
                .getString("sms.service.url")).post();

.post takes (T body, play.api.http.Writeable wrt, play.api.http.ContentTypeOf ct) but I don't understand how I should pass the params there. The documentation only states:

Promise<WS.Response> result = WS.url("http://localhost:9001").post("content");

How do I set the content eg. param1=foo and param2=bar?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try constructing the request like this:

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

The method url(java.lang.String url) returns a WS.WSRequestHolder reference which can be used to modify the original request using chained calls to setQueryParameter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...