Rest-assured: Cannot send a Content-Type form-data request using rest assured

Created on 16 Apr 2018  路  4Comments  路  Source: rest-assured/rest-assured

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)

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"
}

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

Appreciate your support on this.

Thanks.

Most helpful comment

Managed to solve the issue by sending with contentType("multipart/form-data") instead of the form data provided by rest assured. Works exactly like in postman.

Like so:

```
protected Response doPostForm(String url, Map formData) {
RequestSpecification req = startPostRequest();
req.contentType("multipart/form-data");
for (Map.Entry entry : formData.entrySet())
{
String key = entry.getKey();
String value = entry.getValue();
req.multiPart(key, value);
}
return req.post(url);
}

All 4 comments

Hi @osandadeshan, any updates on this? I'm yielding the same, with the same components, and as you said, works totally fine in Postman sending as form-data.

Managed to solve the issue by sending with contentType("multipart/form-data") instead of the form data provided by rest assured. Works exactly like in postman.

Like so:

```
protected Response doPostForm(String url, Map formData) {
RequestSpecification req = startPostRequest();
req.contentType("multipart/form-data");
for (Map.Entry entry : formData.entrySet())
{
String key = entry.getKey();
String value = entry.getValue();
req.multiPart(key, value);
}
return req.post(url);
}

Hi @osandadeshan, I solved the problem in the following way.

RestAssured.given().config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig().encodeContentTypeAs("multipart/form-data", ContentType.TEXT)))
.contentType("multipart/form-data; boundary=--MyBoundary")
.queryParams(params)   // formParams does not working
.post(url)

Managed to solve the issue by sending with contentType("multipart/form-data") instead of the form data provided by rest assured. Works exactly like in postman.

Like so:

protected Response doPostForm(String url, Map<String, String> formData) {
        RequestSpecification req =  startPostRequest();
        req.contentType("multipart/form-data");
        for (Map.Entry<String,String> entry : formData.entrySet())
        {
            String key = entry.getKey();
            String value = entry.getValue();
            req.multiPart(key, value);
        }
        return req.post(url);
}

It works

Was this page helpful?
0 / 5 - 0 ratings