如何使用放心发送内容类型表单数据请求?

时间:2018-04-15 15:08:08

标签: java api automation backend rest-assured

我需要使用Rest Assured调用表单数据类型的API。这是我的代码。

private Map<String, String> getFormParamsMap() {
  Map<String, String> formParams = new HashMap<>();
    formParams.put("creatorId", "Instructor1");
    formParams.put("creatorPlatform", "Web");
    formParams.put("creatoredSource", "File");
    formParams.put("creatoredType", "Auto");
    formParams.put("deckId", "5a605b472e02d86561172dad");
    formParams.put("userId", "kind");
    return formParams;
}

public void invoke() {
          response = given()
                    .header("Content-Type", "application/form-data")
                    .header(AUTHORIZATION_HEADER_NAME, accessToken) //Some API contains access token to run with the API
                    .headers(headers)
                    .formParams(getFormParamsMap()) // requestParamsMap here.
                    .when()
                    .post(invokingEndpoint);
}

当我执行此操作时,我收到以下错误。

Message: java.lang.IllegalArgumentException: Don't know how to encode creatorPlatform=Web&creatoredType=Auto&deckId=5a605b472e02d86561172dad&creatorId=Instructor1&creatoredSource=File&userId=kind as a byte stream.

Please use EncoderConfig (EncoderConfig#encodeContentTypeAs) to specify how to serialize data for this content-type.
For example: "given().config(RestAssured.config().encoderConfig(encoderConfig().encodeContentTypeAs("application/form-data", ContentType.TEXT))). .."
Stack Trace:
io.restassured.internal.http.EncoderRegistry.encodeStream(EncoderRegistry.java:130)

当我在.config(RestAssured.config().encoderConfig(encoderConfig().encodeContentTypeAs("application/form-data", ContentType.TEXT)))方法中使用invoke()时,它会给出如下结果。

{
"status": 400,
    "message": "Content type 'application/x-www-form-urlencoded;charset=ISO-8859-1' not supported",
    "error": "Bad Request",
    "exception": "org.springframework.web.HttpMediaTypeNotSupportedException"
}

我的请求不是x-www-form-urlencoded类型,而是form-data类型。我可以用邮递员执行它。

感谢您对此的支持。

感谢。

3 个答案:

答案 0 :(得分:0)

请添加消费者。

有关Rest Assured可用的编码器,请参阅here。 这可能会导致问题 -

encodeContentTypeAs("application/form-data", ContentType.TEXT)

你也可以试试这个 -

.encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false).encodeContentTypeAs("application/form-data", ContentType.TEXT));

答案 1 :(得分:0)

据我所知,headers(headers)方法替换了所有标头,然后RestAssured使用x-www-form-urlencoded内容类型作为默认值。

尝试添加&#34; Content-Type&#34;调用headers(headers)后的标题。

答案 2 :(得分:0)

我已经使用encodeContentTypeAs("multipart/form-data", ContentType.TEXT)

解决了此问题

例如:-

public void invoke() {
     response = given()
                .config(
                        RestAssured.config()
                                .encoderConfig(
                                        encoderConfig()
                                                .encodeContentTypeAs("multipart/form-data", ContentType.TEXT)))
                .headers(headers)
                .formParams(formParams)
                .when()
                .post(oAuthBaseURI).then().extract().response();
}