Not able to upload Multipart/form-data using Katalon Studio


#1

Hi Team,

I have been trying to upload a file in a post api using katalon scripts but not able to do so. Below is the code that I am using to build my request. But not getting the desired response. Seems like the file is not getting posted.

@Keyword

def static requestBuilder(String url, String method, Map headerFields, String requestBody) {

‘Default contentType is “application/json”’

if (!headerFields.containsKey(‘Content-Type’)) {

headerFields.put(‘Content-Type’, ‘application/json’)

}

List header = new ArrayList()

for (Map.Entry<String, String> headerEntry : headerFields.entrySet()) {

header.add(new TestObjectProperty(headerEntry.getKey(), ConditionType.EQUALS, headerEntry.getValue()))

}

List formDataBdyparam=new ArrayList<>();

formDataBdyparam.add(new FormDataBodyParameter(‘file’,‘FileData\\asdf.html’ , ‘text’))

def builder = new RestRequestObjectBuilder()

//def requestObject = builder.withRestRequestMethod(method).withRestUrl(url).withHttpHeaders(header).withTextBodyContent(requestBody).build()

def requestObject = builder.withRestRequestMethod(method).withRestUrl(url).withHttpHeaders(header).withMultipartFormDataBodyContent(formDataBdyparam).build()

return requestObject

}

Is there any method available to provide the file as a parameter in the FormDataBodyParameter constructor?

I feel I am not passing correct parameters in the FormDataBodyParameter constructor because of which the withMultipartFormDataBodyContent is not working correctly.

I would also like to mention that the request is being posted and I am getting 401 response code back.

Please let me know how to proceed for the same.

Thanks
Anamika


#2

Hi @Anamika_Mukhopadhyay

You could try using cURL via process builder. (We’ve given up using Katalon’s REST builder :frowning: )

ProcessBuilder pb = new ProcessBuilder(“curl”, “-X”, “POST”,
““https://samplesite/index.php?/api/v2/add_attachment_to_result/3996=””,
“-H”, ““Authorization: Basic tHiSiSaToKeN””
, “-H”, ““Content-Type: multipart/form-data””, “-H”, ““cache-control: no-cache””,
“-F”, ““attachment=@C:\test-image.jpg””)
pb.redirectErrorStream(true)
Process p2 = pb.start();