Why do I get Casting exception in my code when using HTTPBuilder

This is the continue from this link: https://community.bonitasoft.com/questions-and-answers/trouble-calling-servlet-upload-file

I manage to find the require libraries.

My current code for calling Bonita's formUpload servlet is as below:

    def uploadFile(String server, String path, String contentType, String fileName) {
        // File upload using pageUpload servlet to a temporary folder
        def uploadResponse
        LOGGER.info("Create HTTPBuilder: " + server)
        HTTPBuilder http = new HTTPBuilder(server)

        http.request(POST, TEXT ) { multipartRequest ->
            // URL of page upload servlet (REST API extension is handle as a page)
            uri.path = "/API/formFileUpload"

            // We use multipart content type to upload the file
            requestContentType = "multipart/form-data"
            LOGGER.info("requestContentType: " + requestContentType)

            // Create a multipart entity builder for our local test file (the SQL data source REST API extension)
            MultipartEntityBuilder multiPartContent = MultipartEntityBuilder.create().addBinaryBody("file", new File(path))
            def entity = multiPartContent.build()
            
            // This code should work, but return casting exception
            multipartRequest.entity = entity

            // Handle successful response
            response.success = { resp, reader ->
                LOGGER.info("Status code: " + resp.statusLine.statusCode)
                if (resp.statusLine.statusCode == 200) {
                    // Store response information that include information about temporary storage of the file on server side
                    LOGGER.info("Response text: " + reader.text)
                    uploadResponse = reader.text
                }
            }
            response.failure = { resp, reader ->
                LOGGER.info("Upload unsuccessful")
            }
        }

        if(uploadResponse == null) {
            throw new Exception("Failed to upload the file")
        } else {
            return uploadResponse
        }
    }

The part "multipartRequest.entity = entity" keep throw casting error, but as I research, this code should work.

Exception:

Cannot cast object 'org.apache.http.entity.mime.MultipartFormEntity@12446679' with class 'org.apache.http.entity.mime.MultipartFormEntity' to class 'org.apache.http.HttpEntity'

Can you guy help me.

Hi,

Where this code is executed ? Is it in a Rest API extension ? If yes, did you declare the http client dependency ?

Yes, it is a part of REST API groovy script.
About dependencies, is it about the jar files put in folder "lib" in REST API folder?
This is the list of jar file:

  • commons-codec-1.2.jar
  • commons-collections-3.2.2.jar
  • commons-logging-1.2.jar
  • http-builder-0.7.1.jar
  • httpclient-4.5.13.jar
  • httpcore-4.4.15.jar
  • json-lib-2.3-jdk15.jar
  • nekohtml-1.9.16.jar
  • xercesImpl-2.9.1.jar
  • xml-resolver-1.2.jar

Or it it @Grap(...) statement?
If it is the @Grap statement, I got "IvySetting" error when using "@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )". Is this one incorrect?

Http client is provided by the runtime and should not be included in the lib folder.
In maven you can define a provided scope on the http-client dependency in the rest api extension pom.xml.

I develop all of my Bonita REST API with Notepad++ unfortunately.

My Bonita version is Community. I know that Enterprise version have support for writing REST API. Is this what I can do with my version?

You can edit pom.xml files with Notepad
What version of Bonita are you using ?

I use version 7.13.
As I check sample project from GitHub, I find these setting in pom.xml:

https://imgur.com/81xgh2C


Is this I need to add?
Do I need other setting?
Where should I put this file in REST API zip file? Same level as page.properties?

Thanks in advance.

REST Api extension tooling is available in the Community edition since 2021.1.
I recommend using it to have a better integration in your Bonita project.
If you want to keep it outside the Studio you can find additional information on how to write rest api extensions in the documentation.

HTH
Romain