本文介绍了如何使用放心发送 Content-Type 表单数据请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to invoke a form-data typed API using Rest Assured. Here is my code.

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);
}

执行此操作时,出现以下错误.

When I execute this, I am getting the below error.

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))) 时() 方法,结果如下.

When I use .config(RestAssured.config().encoderConfig(encoderConfig().encodeContentTypeAs("application/form-data", ContentType.TEXT))) in the invoke() method, it gives the result as below.

{
"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类型.我可以使用邮递员执行它.

My request is not x-www-form-urlencoded type, it is form-data type. I can execute it using postman.

感谢您对此的支持.

谢谢.

推荐答案

我已经通过使用 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();
}

这篇关于如何使用放心发送 Content-Type 表单数据请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 22:36