ClassCastException Thrown when referring json file in request body with content type "multipart/form-data"
Issue:
With content-type:"multipart/form-data" getting java.lang.ClassCastException when referring json file for additional payload in Request Body apart from "files": [ "file:media/test.wav" ]
Request step:
{
"name": "save_config",
"url": "/config",
"operation": "POST",
"request": {
"headers": "${JSON.FILE:standard_headers_media.json}",
"body": {
"files": [
"file:media/test.wav"
],
"metadata": "${JSON.FILE:metadata.json}"
}
},
"assertions": {
"status": 201,
"headers": {
"${correlationId.header}": "$NOT.NULL"
}
}
}
metadata.json
{
"name": "name",
"fileName": "test.wav",
"location": "location",
}
standard_headers_media.json:
{
"Content-Type": "multipart/form-data"
}
Error:
java.lang.RuntimeException: java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class java.lang.String
(java.util.LinkedHashMap and java.lang.String are in module java.base of loader 'bootstrap')
Stactrace:
Caused by: java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class java.lang.String
(java.util.LinkedHashMap and java.lang.String are in module java.base of loader 'bootstrap')
at org.jsmart.zerocode.core.httpclient.utils.FileUploadUtils.buildOtherRequestParams(FileUploadUtils.java:60)
at org.jsmart.zerocode.core.httpclient.BasicHttpClient.createFileUploadRequestBuilder(BasicHttpClient.java:356)
at org.jsmart.zerocode.core.httpclient.BasicHttpClient.createRequestBuilder(BasicHttpClient.java:400)
at org.jsmart.zerocode.core.httpclient.BasicHttpClient.execute(BasicHttpClient.java:126)
at org.jsmart.zerocode.core.engine.executor.JsonServiceExecutorImpl.executeRESTInternal(JsonServiceExecutorImpl.java:131)
at org.jsmart.zerocode.core.engine.executor.JsonServiceExecutorImpl.executeRESTService(JsonServiceExecutorImpl.java:92)
@srinivasa78 , just checking... have you tried this(below replacing the external content) in a manual testing tool or a REST client like Postman?
"body": {
"files": [
"file:media/test.wav"
],
"metadata": {
"name": "name",
"fileName": "test.wav",
"location": "location",
}
}
What does the API server responds with?
@authorjapps My apologies for responding late.
Getting 201 Created response.
Please let me know if it's need to be fixed or something. I want to contribute to this project.
And though I got experience this would be my first open source contribution. (I know I know I am too late :) )
Thanks @srinivasa78 for your ineterest in contribution. Yes, you can try to fix this.
The code is here needs fix:
org.jsmart.zerocode.core.httpclient.utils.FileUploadUtils#buildOtherRequestParams
Please raise a PR once it's stable.
Do you know how to fork etc? Otherwise, here are the guidelines - CONTRIBUTING.md(Click)
Also please join Slack(click to join) channel if you haven't already!
@srinivasa78, did you have a chance to look at it?
Forked the repo, started working on this. Will raise PR when build is stable.
@authorjapps Raised a PR, please let me know if I need to add this specific scenario in the test cases for HTTP. Check https://github.com/authorjapps/zerocode/pull/435
@authorjapps reminder! the PR has been pending for a while. Please can you help to merge it.
PR merged to master.
This fix is available in 1.3.36(and higher release versions) when released.