Cyril Thomas
Cyril Thomas

Reputation: 11

Rest assured gz file upload multipart form data gets 400 bad request

Tried to ask post checking the forms and with multiple approaches.

I am trying to send post request with Request body uploading with .gz file. Tried with multipart form data, Getting 400 bad request as response.

Also tried to load it as binary file as .body (gzfile) - Same error

Tried many ways, not able to identify what I am missing. Using the latest Rest Assured dependency 5.5.0.

Any suggestions\resolutions can help.

Kindly ignore the difference in bearer token, have edited it to look short

My Swagger response Curl

curl -X 'POST' \

'https://32.039.1.471:8299/testingapi/abc' \ 
-H 'accept: application/json' \
-H 'Accept-Encoding: application/gzip' \
-H 'multipart-form-data: multipart/form-data' \ 

-H 'Correlation-Request-Id: 12345'
-H 'Authorization: Bearer eyJhbGcioiJkaXIiLCJlbmмioiJBMjU2QØJDLUHTNTEyIiwidHlwIjoislduIiwiY3R51joislduino.' \
-H 'Content-Type: multipart/form-data' \
-F '[email protected];type=application/x-gzip'
Request URL
https://serverip:port/testingapi/abc

My rest assured code

token = "Bearer eyJhbGcioiJkaXIiLCJlbmMiOiJBMjU2Q0JDLUHTN";
File gzFile new File(System.getProperty("user.dir")+"/abcDataUpdated.json.gz");
RestAssured.useRelaxedHTTPSValidation();
Response response = given()
.log().all()
.when()
.header("accept", "application/json")
.header("Accept-Encoding", "application/gzip")
.header("multipart-form-data","multipart/form-data")
.header("Correlation-Request-Id","123456")
.header("Authorization", token)
.header("Content-Type", "multipart/form-data") .body (gzFile)

.multiPart("File", gzFile, "application/x-gzip") 
//.contentType (ContentType.MULTIPART)
.post("https://serverip:port/testingapi/abc")
.then() 
.log().all()
.extract().response();

My Response log details in Intellij Run console,

Headers:

accept-application/json
Accept-Encoding=application/gzip
Correlation-Request-Id=123456
Authorization=Bearer eyJhbGci0iJkaXIiLCJlbmMiOiJBMjU2Q0JDLUHTNTEyIiwidHlwIjoisldUIiwiY3R5Ijois
Cookies:
<none>
Content-Type-multipart/form-data
`Multiparts:

Content-Disposition: form-data; name = File; filename = abcDataUpdated.json.gz
Content-Type: application/x-gzip`

My response status and message received with above code

400 Bad Request - Request Badly formed

In Swagger the file upload section is in request body as,

File            (Choose File upload Button)
string($binary)

Upvotes: 1

Views: 63

Answers (1)

Cyril Thomas
Cyril Thomas

Reputation: 11

Resolved the issue, Its a problem with the Accept Encoding from API side, .header("Accept-Encoding", "application/gzip")

instead of application/gzip, it has to be gzip, deflate.

Admin, please close this post!

Upvotes: 0

Related Questions