# Upload file using REST API

**URL:** <https://forum.katalon.com/t/upload-file-using-rest-api/47252>\
**Category:** API Testing\
**Tags:** api-testing, katalon-studio\
**Created:** [September 10, 2020, 12:31pm UTC](https://forum.katalon.com/t/upload-file-using-rest-api/47252 "2020-09-10T12:31:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![susana.maria.repeto](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/susana.maria.repeto/32/16103_2.png) [@susana.maria.repeto](https://forum.katalon.com/u/susana.maria.repeto)\
**Post date:** [September 10, 2020, 12:31pm UTC](https://forum.katalon.com/t/upload-file-using-rest-api/47252/1 "2020-09-10T12:31:57Z")

</div>

Hi everyone!

I’ve searched for this issue on the community and seems to be discussed a couple of times but with no real resolution.

I’m trying to upload an XML file using a POST request and form-data, but I get the following error response:

```
{
  "error":"The results file is required."
}

```

If I use curl it works fine. Also works fine with Postman.  
My real intention is to create the request manually (using a script and `withMultipartFormDataBodyContent()`) but first I wanted to make sure it works using ObjectRepository.

Can someone please help me with this?

Thanks.

---

<div class="post-metadata">

**Author:** ![susana.maria.repeto](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/susana.maria.repeto/32/16103_2.png) [@susana.maria.repeto](https://forum.katalon.com/u/susana.maria.repeto)\
**Post date:** [September 22, 2020, 1:16pm UTC](https://forum.katalon.com/t/upload-file-using-rest-api/47252/2 "2020-09-22T13:16:37Z")

</div>

After a looooooooooooong time of searching and trying different things I already found the solution (that works for me). It uses Okhttp library so you will need to import it.  
If anyone else need it, there it is:

```auto
	public void importJUnitTestExecRequest() {
		
		OkHttpClient client = new OkHttpClient();
		String reportFolder = GlobalVariable.reportFolder + "\\JUnit_Report.xml";
		File file = new File(reportFolder);

		String url = GlobalVariable.importTestExecJUnitEndpoint+"?testExecKey="+GlobalVariable.testExecKey;

		//Form request body that will be a multipart
		RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
				.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("text/xml"), file))
				.build();

		//Form the actual request adding necessary headers
		Request request = new Request.Builder()
				.url(url)
				.post(requestBody)
				.addHeader("Content-Type", GlobalVariable.contentTypeMultipart)
				.build();

		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();
		}

	}

```

You are welcome.

---

<div class="post-metadata">

**Author:** ![anon46315158](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@anon46315158](https://forum.katalon.com/u/anon46315158)\
**Post date:** [September 22, 2020, 2:45pm UTC](https://forum.katalon.com/t/upload-file-using-rest-api/47252/3 "2020-09-22T14:45:38Z")

</div>

> [@susana.maria.repeto](#):
>
> String reportFolder = GlobalVariable.reportFolder + “\JUnit\_Report.xml”;

this is not a folder, but a file 😛  
LE: sorry but i cannot hold it due to work habits, i am paranoid at code review

---

<div class="post-metadata">

**Author:** ![susana.maria.repeto](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/susana.maria.repeto/32/16103_2.png) [@susana.maria.repeto](https://forum.katalon.com/u/susana.maria.repeto)\
**Post date:** [September 22, 2020, 2:51pm UTC](https://forum.katalon.com/t/upload-file-using-rest-api/47252/4 "2020-09-22T14:51:00Z")

</div>

Uhg that’s true.  
Probably I copied&pasted the name, that’s beacuse 😅

Changed it, thanks!

---

<div class="post-metadata">

**Author:** ![mqamar](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/mqamar/32/16104_2.png) [@mqamar](https://forum.katalon.com/u/mqamar)\
**Post date:** [October 7, 2021, 4:25pm UTC](https://forum.katalon.com/t/upload-file-using-rest-api/47252/5 "2021-10-07T16:25:53Z")

</div>

Hi,

Can you(susana.maria.repeto) please explain what is under your GlobalVariable.contentTypeMultipart? No matter what I have tried, this code doesn’t work for me.

Please advise

---

<div class="post-metadata">

**Author:** ![tim.n](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/tim.n/32/16104_2.png) [@tim.n](https://forum.katalon.com/u/tim.n)\
**Post date:** [June 3, 2022, 8:36am UTC](https://forum.katalon.com/t/upload-file-using-rest-api/47252/6 "2022-06-03T08:36:37Z")

</div>

I applied this one and can upload file successfully:  
def requestObject = builder  
.withRestRequestMethod(“POST”)  
.withRestUrl("${GlobalVariable.baseUrl}/file/v2/files")  
.withHttpHeaders([  
new TestObjectProperty(“Content-Type”, ConditionType.EQUALS, “multipart/form-data”),  
new TestObjectProperty(“accept”, ConditionType.EQUALS, “_/_”),  
new TestObjectProperty(“Authorization”, ConditionType.EQUALS, “Bearer ${token}”)  
])  
.withMultipartFormDataBodyContent([  
new FormDataBodyParameter(“file”, file.toString(), FormDataBodyParameter.PARAM\_TYPE\_FILE)  
])  
.build()

```
	def response = WS.sendRequest(requestObject)
```
