File System Content

In following examples backend expects a file passed as multipart/form-data . File content is expected to be stored in file field. Backend responds with received file name and file description.To POST form data, you need to use the same http.post statement as you saw in previous examples. Second parameter should be http.formData instead of a map payload we used for JSON . def imagePath = testResourcePath("src/test/resources/image.png") http.post("/file-upload", http.formData(file: imagePath)) { fileName.should == 'image.png' } Use http.formFile to override file name that is being sent to the backend. def imagePath = testResourcePath("src/test/resources/image.png") http.post("/file-upload", http.formData(file: http.formFile('myFileName.png', imagePath))) { fileName.should == 'myFileName.png' } Multiple form fields can be specified like in the example below. def imagePath = testResourcePath("src/test/resources/image.png") http.post("/file-upload", http.formData(file: imagePath, fileDescription: 'new report')) { fileName.should == 'image.png' description.should == 'new report' }

In-Memory Content

If your test already has content, you can explicitly pass it as is. byte[] fileContent = [1, 2, 3, 4] as byte[] http.post("/file-upload", http.formData(file: fileContent)) { fileName.should == 'backend-generated-name-as-no-name-provided' } Note: no file name is passed and this particular backend generated file name on your behalf.Use http.formFile to provide a file name byte[] fileContent = [1, 2, 3, 4] as byte[] http.post("/file-upload", http.formData( file: http.formFile('myFileName.dat', fileContent))) { fileName.should == 'myFileName.dat' }