如何放心发布x-www-urlencode请求

Wil*_*ams 9 rest-assured postman

我有一个发布请求,我需要发送 x-www-form-urlencoded keyValue 对参数,内容类型应该是 x-www-form-urlencoded。

\n\n

在编码之前,我已经在邮递员中成功尝试过,只需添加 Header"Content-Type=application/x-www-form-urlencoded" 和 x-www-form-urlencoded body 。

\n\n

这是我的代码:`

\n\n
 RestAssured.baseURI="****"\n        RequestSpecification request = RestAssured.given().config(RestAssured.config()\n                .encoderConfig(EncoderConfig.encoderConfig()\n                .encodeContentTypeAs("x-www-form-urlencoded",\n                 ContentType.URLENC)))\n                .contentType(ContentType.URLENC.withCharset("UTF-8"))\n                .formParam("grant_type", *)\n                .formParam("code", *)\n                .formParam("client_id",*)\n                .when().log().all()\n                .then().log().all().request()\n        request.post("/oauth2/token")`\n
Run Code Online (Sandbox Code Playgroud)\n\n

我想放心发布为 formParam 而不是“x-www-form-urlencoded”?\n这是放心日志:`

\n\n
Request method: POST\nRequest URI:    ***\nProxy:          <none>\nRequest params: <none>\nQuery params:   <none>\nForm params:    grant_type=***\n                code=***\n                client_id=***\nPath params:    <none>\nHeaders:        Accept=image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap\n                Content-Type=application/x-www-form-urlencoded; charset=UTF-8\nCookies:        <none>\nMultiparts:     <none>\nBody:           <none>\nHTTP/1.1 405 Method Not Allowed\nContent-Length: 61\nDate: Tue, 30 Jan 2018 06:59:20 GMT\nX-Correlationid: 5d155b6f-0d85-4775-5f50-82c397e5b44b\nX-Smp-Log-Correlation-Id: 5d155b6f-0d85-4775-5f50-82c397e5b44b\nX-Vcap-Request-Id: 5d155b6f-0d85-4775-5f50-82c397e5b44b\nOnly support Content-Type\xef\xbc\x9aapplication/x-www-form-urlencoded\n
Run Code Online (Sandbox Code Playgroud)\n\n

\n这个问题让我发疯了好几天。\n请告诉我是否有其他方法可以发送 x-www-form-urlencoded 参数或代码中需要的一些更新。

\n\n

多谢!

\n

小智 16

Response response = RestAssured
    .given()
    .contentType("application/x-www-form-urlencoded; charset=utf-8")
        .formParam("grant_type", "password")
        .formParam("username", user_email)
        .formParam("password", user_password)
        .formParam("audience", audience)
        .formParam("scope", "openid email")
        .formParam("client_id", REGULAR_APP_CLIENT_ID)
        .formParam("client_secret", REGULAR_APP_SECRET_ID)
    .when()
        .post(AUTH0_URL);
Run Code Online (Sandbox Code Playgroud)


Rom*_*dov 4

如果您需要发送正文中带有参数的请求:

String body = String.format("grant_type=%s&code=%s&clientid=%s", grantType, code, clientId);
Response response = given().with().
header("Content-Type", "application/x-www-form-urlencoded").
body(body).
post("/oauth2/token");
Run Code Online (Sandbox Code Playgroud)

Case for params in URL:

Response response = given().with().
header("Content-Type", "application/x-www-form-urlencoded").
post("/oauth2/token?grant_type={type}&code={code}&clientid={id}");
Run Code Online (Sandbox Code Playgroud)

Case for params in header (also might use Header object io.restassured.http.Header):

Response response = given().with().
header("Content-Type", "application/x-www-form-urlencoded").
header("grant_type", type).
header("code", code).
header("clientid", id).
post("/oauth2/token");
Run Code Online (Sandbox Code Playgroud)

BTW use static give() for don't duplicate Config

public static RequestSpecification given() {
RestAssured.config = RestAssured.config().
...;
return given().baseUrl(BASE_URL).contentType(ContentType.URLENC);
}
Run Code Online (Sandbox Code Playgroud)