Error while using withFileBodycontent(String filepath)


#1

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?


#2

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)

}


#3

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