groovymultipartform-datahttpbuilder

http-builder in groovy uploading multiple files in server


I am trying to upload multiple files in a server using http builder-groovy.The below code doesn't works.Getting an error " Request entity is too large" .I have imported all the packages and defined all the variables.

Is there any alternative for this using restclient-groovy?

can anyone give the reason?

thanks in advance.

def file = new File("resources/IMG.JPG")
def file1 = new File("resources/aa.json")

http = new HTTPBuilder( url )

http.request (POST, JSON) { multipartRequest ->

      uri.path = '/server/upload'
      uri.query = [param1:value, param2:value, param3:value, param4:value]

      requestContentType = 'multipart/form-data'

      MultipartEntity mpe = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE)
      mpe.addPart( "jpeg", new FileBody(( File ) file , 'image/jpeg' ))
      mpe.addPart( "json", new FileBody(( File ) file1 , 'application/json' ))

      multipartRequest.setEntity(mpe)

      response.success = { resp, json->
          println "POST response status: ${resp.statusLine}"
          println "Query response: ${json}"
      }

      response.failure = {  resp ->
          println "POST response statusline: ${resp.statusLine}"
      }
}

Solution

  • You could actually use RestAssured framework to form multipart requests with ease.

    Below is an example,

    @Test
    public void multiExample()
    {
        given().
            multiPart("image", new File("resources/a.jpg"), "image/jpeg").
            multiPart("lgdata", new File("resources/myfile.json"), "application/json").
        expect().
            body("result", is("OK")).
        when().
            post(url);
    
    }