在放心的情况下,如何设置url编码的表单实体并向表单实体添加参数?

vin*_*tty 3 api rest rest-assured apache-httpclient-4.x

下面的示例代码在http客户端中,但是我想在Rest Assured中编写相同的代码。我知道我们也可以放心使用http库,但是我想放心使用

HttpPost pst = new HttpPost(baseUrl, "j_spring_security_check"))
pst.setHeader("Content-Type", "application/x-www-form-urlencoded")
ArrayList<NameValuePair> postParam = new ArrayList<NameValuePair>()
postParam .add(new BasicNameValuePair("j_username",username))
postParam .add(new BasicNameValuePair("j_password",password))
UrlEncodedFormEntity formEntity23 = new UrlEncodedFormEntity(postParam)
pst.setEntity(formEntity23 )
HttpResponse response = httpclient.execute(pst);
Run Code Online (Sandbox Code Playgroud)

小智 5

对于“放心的人”,您可以使用以下代码段。

Response response = RestAssured.given().header("Content-Type", "application/x-www-form-urlencoded").formParam("j_username", "uName").formParam("j_password", "pwd").request().post(url);
Run Code Online (Sandbox Code Playgroud)

由于您的应用程序使用的是表单url编码的内容类型,因此您可以如上所述将Header类型设置为此。

希望对您有帮助。