Upload formdata is not working
Headers as

Hi!
That’s an issue I have also recently experienced. The only solution that worked for me is on this thread: Upload File API
But it seems you’re missing something in your request, the Borrower Data. You should send it also in the form-data as a parameter I guess.
Try that.
Thanks Susana, Let me try that link solution and update you,
Well the borrower data is nothing already passing formdata (with same parameters its working in postman) @susana.maria.repeto Did you make it work at Object repository or in TestScript ? (sorry for being naive on this)
If it’s working on postman I can tell you for sure it’s going to work in Katalon, but maybe you’ll have to make your own code like me.
tip: check the headers generated in Postman, the will give you a hint
Yep copied all headers but yes the code might work… thanks again
Hi @susana.maria.repeto
after importing okhttp 4.9.0 jar, restarted the katalon.I am getting this error in my test script
@ line 17, column 1.
import com.squareup.okhttp
commenting the above i am getting
unable to resolve class OkHttpClient
@ line 24, column 14.
OkHttpClient client = new OkHttpClient().newBuilder()
what is missing ? any way-around
Got resolved by importing the above class silly
Now facing another error
unable to resolve class MediaType
MediaType mediaType = MediaType.parse("multipart/form-data");
have to import this too ?
Uhm I don’t have my code in front of me right now, so I’m not 100% sure but I think I didn’t use an external library, you can simple put this on your script imports:
import okhttp3.*;
A little later I can check if that doesn’t work!
yes imported okhttp3.* works for previous issue. Sure no worries let me know later. thanks
Okay here I am again.
MediaType is from okhttp so if you put the line I posted above it would work. I think Katalon itself has some library with the same Object, make sure it is not taking the wrong one.
But let me ask you a question, because maybe we are going by the long route with this code and it can be done easier than that. What kind of file do you want to upload? (PDF, XML, .jpg…) and what’s the purpose?
Katalon also provides some classes to create API requests in scripting mode (not ObjectRepository). I gave you the code that uses OkHttp because once again, Katalon didn’t work with its own classes, at least for me.
Try also this code that is simpler, maybe it will work for you (of course you’ll have to change headers to match yours):
TestObjectProperty contentTypeHeader = new TestObjectProperty("Content-Type", ConditionType.EQUALS, "multipart/form-data")
basicAuthHeader = new TestObjectProperty("Authorization",ConditionType.EQUALS, "Basic myBase64token")
defaultHeaders = Arrays.asList(cookieHeader, contentTypeHeader)
RequestObject ro = new RestRequestObjectBuilder()
.withRestUrl(importTestExecJUnitEndpoint)
.withHttpHeaders(defaultHeaders)
.withRestRequestMethod("POST")
.withMultipartFormDataBodyContent([new FormDataBodyParameter('afile',filePath,'PARAM_TYPE_FILE')])
.build()
ResponseObject respObj = WS.sendRequest(ro)
You should also check this link that explains much better than me the WS scripting API requests mannualy
Let me know!
I am currently sending .txt (but lately .jpg . jpeg, .tiff , .xls will come in my tea case ) It basically do some selfsigned docs need for approval for same Itemid iam passing along with.
Thanks for your reply … Will surely update on its working
Hi No Sucess on this
i use both codes separately in the tests it did get passed without any failure. But when check the HAR file for response get same “405” error like i am getting in Object repository
"response" : {
"status" : 415,
"statusText" : "",
"httpVersion" : "",
"cookies" : [ ],
"headers" : [ {
"name" : "Transfer-Encoding",
"value" : "chunked"
},
Postman Request works fine
Hi!!
415 error typically is related to the media type you’re trying to upload, that is not supported.
Can you please post your full code and also the full HAR file?
Cheers!
Test Script Code is (this
WS.sendRequest(findTestObject('TPO/TPO - Upload Document', [('RESOURCE_PREFIX_TPO') : GlobalVariable.RESOURCE_PREFIX_TPO
, ('ACCESS_TOKEN') : GlobalVariable.ACCESS_TOKEN,
public void importJUnitTestExecRequest()
{
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("multipart/form-data;");
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("loanguid", "33384d20-1597-43d8-8d56-164f9f630097")
.addFormDataPart("file","/C:/Users/pathtoupload/qaauto-tpo/test.txt",
RequestBody.create(MediaType.parse("application/octet-stream"),
new File("/C:/pathtoupload/test.txt")))
.addFormDataPart("documentName", "underwriting")
.addFormDataPart("documentType", "FHA")
.addFormDataPart("applicationid", "ALL")
.build();
Request request = new Request.Builder()
.url("MyURL/attachment")
.method("POST", body)
.addHeader("Authorization", "Bearer ${ACCESS_TOKEN}")
.addHeader("Content-Type", "multipart/form-data; boundary=<calculated when request is sent>")
.addHeader("Accept-Encoding", "gzip, deflate, br")
.addHeader("Content-Length", "calculated when request is sent>")
.addHeader("Accept", "*/*")
.build();
//Response response = client.newCall(request).execute();
//println("Response status: " + response);
//String responseBody = response.body().string();
//println("************ IMPORT TEST EXECUTION RESULTS RESPONSE BODY ************");
//println(responseBody);
//println("*********************************************************************");
//def slurper = new groovy.json.JsonSlurper()
//def result = slurper.parseText(response.getResponseBodyContent())
//println(result)
Response response = null;
//
try {
response = client.newCall(request).execute();
println("************ IMPORT TEST EXECUTION RESULTS RAW RESPONSE ************");
println("Response status: " + response);
println("********************************************************************");
if (response.isSuccessful()){
String responseBody = response.body().string();
println("************ IMPORT TEST EXECUTION RESULTS RESPONSE BODY ************");
println(responseBody);
println("*********************************************************************");
} else {
throw new IOException("Unexpected HTTP code " + response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
HAR file: Please check your inbox, shared already. Thanks
Not the most well-versed in API, so I’ll tag someone who knows more about this @huynguyen
Hello!
Okay I’m not sure about anything anymore but try this. I’ve changed the following:
WS.sendRequest(findTestObject('TPO/TPO - Upload Document', [('RESOURCE_PREFIX_TPO') : GlobalVariable.RESOURCE_PREFIX_TPO, ('ACCESS_TOKEN') : GlobalVariable.ACCESS_TOKEN,
public void importJUnitTestExecRequest() {
OkHttpClient client = new OkHttpClient();
RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("loanguid", "33384d20-1597-43d8-8d56-164f9f630097")
.addFormDataPart("file","C:/Users/pathtoupload/qaauto-tpo/test.txt", RequestBody.create(MediaType.parse("application/text"), new File("C:/pathtoupload/test.txt")))
.addFormDataPart("documentName", "underwriting")
.addFormDataPart("documentType", "FHA")
.addFormDataPart("applicationid", "ALL")
.build();
Request request = new Request.Builder()
.url("MyURL/attachment")
.post(body)
.addHeader("Authorization", "Bearer ${ACCESS_TOKEN}")
.addHeader("Content-Type", "multipart/form-data")
.build();
//Response response = client.newCall(request).execute();
//println("Response status: " + response);
//String responseBody = response.body().string();
//println("************ IMPORT TEST EXECUTION RESULTS RESPONSE BODY ************");
//println(responseBody);
//println("*********************************************************************");
//def slurper = new groovy.json.JsonSlurper()
//def result = slurper.parseText(response.getResponseBodyContent())
//println(result)
Response response = null;
//
try {
response = client.newCall(request).execute();
println("************ IMPORT TEST EXECUTION RESULTS RAW RESPONSE ************");
println("Response status: " + response);
println("********************************************************************");
if (response.isSuccessful()){
String responseBody = response.body().string();
println("************ IMPORT TEST EXECUTION RESULTS RESPONSE BODY ************");
println(responseBody);
println("*********************************************************************");
} else {
throw new IOException("Unexpected HTTP code " + response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
Hope it works!!
Hi @gsp,
Please send the whole HAR file for investigation.
Hi Pls check inbox, HAR file shared too, Susana is having the same