Error while using withFileBodycontent(String filepath)

Hi,
I created a request builder for uploading a file, but i am getting the below response

{

“status” : 500,

“error” : “Internal Server Error”,

“exception” : “org.springframework.web.multipart.MultipartException”,

“message” : “Current request is not a multipart request”

}

Below is my Code:

import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase

import com.kms.katalon.core.testobject.ConditionType

import com.kms.katalon.core.testobject.RestRequestObjectBuilder

import com.kms.katalon.core.testobject.TestObjectProperty

import com.kms.katalon.core.util.KeywordUtil

import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

String AccessTokenForLogin = WebUI.callTestCase(findTestCase(‘API Testing/UserRegistration/getAccessTkn’), [‘urlLogin’: urlLogin, ‘userName’: userName, ‘pwd’: pwd])

println(AccessTokenForLogin)

authorizationKeyValue = WebUI.concatenate(((['Bearer ', AccessTokenForLogin]) as String[]))

String postUrl = “${baseUrlForUpload}/upload/scorecard”

String supplierID = supplierIDforScoreCard

String filepath = filePathForScoreCard

def RequestBuilder = new RestRequestObjectBuilder()

def requestObj = RequestBuilder

.withRestRequestMethod(“POST”)

.withRestUrl(postUrl)

.withFileBodyContent(filepath)

.withTextBodyContent(supplierID)

.withHttpHeaders([

new TestObjectProperty(“Content-Type”, ConditionType.EQUALS, “application/json”),

new TestObjectProperty(“Authorization”, ConditionType.EQUALS, authorizationKeyValue),

])

.build()

Response1 = WS.sendRequest(requestObj)

Response_Code = Response1.getStatusCode()

responseMessage = Response1.getResponseText()

println(responseMessage)

if (Response_Code != 200){

KeywordUtil.markFailed("Status code is not 200 as expected. It is "+Response_Code)

}

Can someone please help?

Resolved the issue. Please find below the updated code for anyone in need.

import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase

import org.apache.http.client.methods.RequestBuilder

import com.kms.katalon.core.testobject.ConditionType

import com.kms.katalon.core.testobject.FormDataBodyParameter

import com.kms.katalon.core.testobject.RestRequestObjectBuilder

import com.kms.katalon.core.testobject.TestObjectProperty

import com.kms.katalon.core.util.KeywordUtil

import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

String AccessTokenForLogin = WebUI.callTestCase(findTestCase(‘API Testing/UserRegistration/getAccessTkn’), [‘urlLogin’: urlLogin, ‘userName’: userName, ‘pwd’: pwd])

println(AccessTokenForLogin)

authorizationKeyValue = WebUI.concatenate(((['Bearer ', AccessTokenForLogin]) as String[]))

println(authorizationKeyValue)

String postUrl = “${baseUrlForUpload}/upload/scorecard”

String supplierID = supplierIDforScoreCard

String filepath = filePathForScoreCard

def RequestBuilder = new RestRequestObjectBuilder()

def requestObj = RequestBuilder

.withRestRequestMethod(“POST”)

.withRestUrl(postUrl)

.withHttpHeaders([

new TestObjectProperty(“Content-Type”, ConditionType.EQUALS, “multipart/form-data”),

new TestObjectProperty(“Authorization”, ConditionType.EQUALS, authorizationKeyValue),

])

.withMultipartFormDataBodyContent([

new FormDataBodyParameter(‘file’,"${filePathForScoreCard}",‘File’),

new FormDataBodyParameter(‘supplierId’,"${supplierIDforScoreCard}",‘PARAM_TYPE_TEXT’),

])

.build()

Response1 = WS.sendRequest(requestObj)

Response_Code = Response1.getStatusCode()

responseMessage = Response1.getResponseText()

println(responseMessage)

println(Response_Code)

if (Response_Code != 200){

KeywordUtil.markFailed("Status code is not 200 as expected. It is "+Response_Code)

}

Hi,

I am facing similar kind of issue and your solution didn’t solve it either… I noticed that my ‘Content-type’ parameter needs ‘boundary’ to it. any idea? please help

Did you guys came up with a solution to this issue? We are getting an error where the server expects Boundary and we do not know how to get around it.

1 Like

Hi @david.casiano ,
Did you use multipart/form-data content type for your request body?

Yes. If I put just multipart/form-data. The server will complain with "no multipart boundary param in Content-type.
If I put a random boundary, the server will complain with “multipart: NextPart: EOF”.
The powershell script that uses System.Net.Http.MultipartFormDataContent seems to work without a hitch. Please advice.

1 Like

Hi @david.casiano,
Please send the HAR file generated when sending the request.

3.har (2.4 KB)
I attached the edited HAR file(so it won’t contain some private data-only edited the posted json file). The response to this request was “multipart: NextPart: EOF”. The request used was by Object Repository functionality in the Katalon providing only the Content-Type with an HTTP Body of file.

Hi @david.casiano,
Please send the original HAR file without editing. To make it private, you can send via personal messages.

I’m getting an error “Cannot send a personal message to that user” if I try the personal message feature.

Hi @david.casiano,
I have sent a private message to you.

Hi @david.casiano,
I found no boundary in the request body after inspecting the HAR file you shared. It may be the cause for your issue, as the server cannot detect the boundary used to distinguish among parts in the request body.
If you construct your request object via script, please send the script for troubleshooting. If you define your request object via Object Repository, please send the request file (.rs file) for further investigation.