# Facing Issue with testing File Upload API using robot framework

**URL:** <https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254>\
**Category:** RequestsLibrary\
**Created:** [21 May 2020 16:07 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254 "2020-05-21T16:07:58Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![SuperSonic](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@SuperSonic](https://forum.robotframework.org/u/SuperSonic)\
**Post date:** [21 May 2020 16:07 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/1 "2020-05-21T16:07:58Z")

</div>

I am very new to testing / developing. I am currently testing a REST API. In Postman I send a POST request by specifying parameters and uploading a file using form-data in the body. My request always succeeds in postman. However, when I try to translate this into Robot Framework (using the Requests Library) file is not getting selected in the request body(displaying content present in a file in the body).  
I am posting my code below.  
I have been trying to resolve this issue for a while now. Any help in the right direction would be much appreciated!

\*\*\* Test Cases \*\*\*  
TC001

```
 create session mysession ${uri}
   
  ${file_name}= Get Binary File ${CURDIR}//test.txt
  
  ${asset}= Create Dictionary asset={file_name} 
     
  
 ${header}= create dictionary Accept=text/plain Content-Type=multipart/form-data boundary    

${response}= post request mysession /api/insertFile data=${file_name} headers=${header}
log to console ${response.content}
  #to validate status code with the actual status code
 ${Status_code}= convert to string ${response.status_code}
 should be equal ${Status_code} 200

 #to validate the content of the response body
 ${response_body}= convert to string ${response.content}
 should contain ${response_body} true

```

Response -  
20200521 21:36:58.725 : INFO : Creating Session using : alias=mysession, url=https://someurl.com, headers={}, cookies={}, auth=None, timeout=None, proxies=None, verify=False, debug=0  
20200521 21:36:58.727 : INFO : Getting file ‘C:\Users\fl792\eclipse-workspace\APITesting\API\test.txt’.  
20200521 21:36:58.727 : INFO : {file\_name} = lsjkgbsdkjb 20200521 21:36:58.729 : INFO : {asset} = {‘asset’: ‘{file\_name}’}  
20200521 21:36:58.730 : INFO : ${header} = {‘Accept’: ‘text/plain’, ‘Content-Type’: ‘multipart/form-data boundary’}  
20200521 21:37:00.863 : INFO : POST Request : url=https://someurl/api/insertFile  
path\_url=/api/insertFile  
headers={‘User-Agent’: ‘python-requests/2.23.0’, ‘Accept-Encoding’: ‘gzip, deflate’, ‘Accept’: ‘text/plain’, ‘Connection’: ‘keep-alive’, ‘Content-Type’: ‘multipart/form-data boundary’, ‘Content-Length’: ‘12’}  
body=b’lsjkgbsdkjb ’

20200521 21:37:00.865 : INFO : POST Response : url=https://someurl/api/insertFile  
status=200, reason=OK  
body=false

---

<div class="post-metadata">

**Author:** ![lucagiove](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/lucagiove/32/85_2.png) [@lucagiove](https://forum.robotframework.org/u/lucagiove)\
**Post date:** [21 May 2020 20:04 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/2 "2020-05-21T20:04:13Z")

</div>

Multipart should not be needed if you want to upload one single file.  
Could you try with this new keywords instead of Get Binary File?  
[https://marketsquare.github.io/robotframework-requests/doc/RequestsLibrary.html#Get%20File%20For%20Streaming%20Upload](https://marketsquare.github.io/robotframework-requests/doc/RequestsLibrary.html#Get%20File%20For%20Streaming%20Upload)

Let me know.

---

<div class="post-metadata">

**Author:** ![SuperSonic](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@SuperSonic](https://forum.robotframework.org/u/SuperSonic)\
**Post date:** [21 May 2020 21:17 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/3 "2020-05-21T21:17:10Z")

</div>

Thanks for the help. I tried this solution but still that file not getting uploaded

This API is working fine in swagger and postman

curl -X POST “[https://some.com/api/insertFile](https://some.com/api/insertFile)” -H “accept: text/plain” -H “Content-Type: multipart/form-data” -F “asset=@Albany\_Accomodations.pdf;type=application/pdf”  
Curl URL

below is my code with new keyword

create session mysession ${uri}

```
${filepath} Get File For Streaming Upload ${CURDIR}${/}test.txt
${header}= create dictionary Accept=text/plain Content-Type=multipart/form-data boundary   
${response}= post request mysession /api/insertFile asset=${filepath} headers=${header}  
log to console ${response.content}
  #to validate status code with the actual status code
 ${Status_code}= convert to string ${response.status_code}
 should be equal ${Status_code} 200
 #to validate the content of the response body
 ${response_body}= convert to string ${response.content}
 should contain ${response_body} true

```

Server Log  
20200522 02:39:57.418 : INFO : Creating Session using : alias=mysession, url=https://somurl.com, headers={}, cookies={}, auth=None, timeout=None, proxies=None, verify=False, debug=0  
20200522 02:39:57.420 : INFO : {filepath} = \<\_io.BufferedReader name='C:\\Users\\fl792\\eclipse-workspace\\APITesting\\API\\test.txt'\> 20200522 02:39:57.424 : INFO : {header} = {‘Accept’: ‘text/plain’, ‘Content-Type’: ‘multipart/form-data boundary’}  
20200522 02:39:58.791 : INFO : POST Request : url=https://somurl.com/api/insertFile  
path\_url=/api/insertFile  
headers={‘User-Agent’: ‘python-requests/2.23.0’, ‘Accept-Encoding’: ‘gzip, deflate’, ‘Accept’: ‘text/plain’, ‘Connection’: ‘keep-alive’, ‘Content-Type’: ‘multipart/form-data boundary’, ‘Content-Length’: ‘96’}  
body=asset=\<\_io.BufferedReader name=‘C:\Users\fl792\eclipse-workspace\APITesting\API\test.txt’\>  
20200522 02:39:58.793 : INFO : POST Response : url=https://somurl.com/api/insertFile  
status=200, reason=OK  
body=false  
20200522 02:39:58.796 : INFO : {response} = \<Response [200]\> 20200522 02:39:58.809 : INFO : {Status\_code} = 200  
20200522 02:39:58.820 : INFO : ${response\_body} = false  
20200522 02:39:58.826 : FAIL : ‘false’ does not contain ‘true’

---

<div class="post-metadata">

**Author:** ![lucagiove](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/lucagiove/32/85_2.png) [@lucagiove](https://forum.robotframework.org/u/lucagiove)\
**Post date:** [21 May 2020 22:01 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/4 "2020-05-21T22:01:06Z")

</div>

> [@SuperSonic](#):
>
> `post request mysession /api/insertFile asset=${filepath} headers=${header}`

You should change asset= with data=

---

<div class="post-metadata">

**Author:** ![SuperSonic](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@SuperSonic](https://forum.robotframework.org/u/SuperSonic)\
**Post date:** [22 May 2020 04:24 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/5 "2020-05-22T04:24:43Z")

</div>

{response}= post request mysession /api/insertFile data={filepath} headers=${header}

Changed and verified still getting false response

---

<div class="post-metadata">

**Author:** ![SuperSonic](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@SuperSonic](https://forum.robotframework.org/u/SuperSonic)\
**Post date:** [22 May 2020 13:06 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/6 "2020-05-22T13:06:40Z")

</div>

Server response  
POST Request : url=https://someurl.com/api/insertFile  
path\_url=/api/insertFile  
headers={‘User-Agent’: ‘python-requests/2.23.0’, ‘Accept-Encoding’: ‘gzip, deflate’, ‘Accept’: ‘text/plain’, ‘Connection’: ‘keep-alive’, ‘Content-Type’: ‘multipart/form-data boundary’, ‘Content-Length’: ‘12’}  
body=\<\_io.BufferedReader name=‘C:\Users\fl792\eclipse-workspace\APITesting\API\test.txt’\>

12:38:02.520 INFO POST Response : url=https://someurl.com/api/insertFile  
status=200, reason=OK  
body=false

---

<div class="post-metadata">

**Author:** ![mikahanninen](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/mikahanninen/32/62_2.png) [@mikahanninen](https://forum.robotframework.org/u/mikahanninen)\
**Post date:** [22 May 2020 14:41 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/7 "2020-05-22T14:41:35Z")

</div>

@SuperSonic so status=200 and reason=OK does not indicate successful upload ? are you sure that there should be something in the body ? and you have checked that file was not actually uploaded ? 😉 … just confirming some things

---

<div class="post-metadata">

**Author:** ![SuperSonic](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@SuperSonic](https://forum.robotframework.org/u/SuperSonic)\
**Post date:** [22 May 2020 15:53 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/8 "2020-05-22T15:53:55Z")

</div>

Yes there is something in the body not sure what is, I should get a response as **true** but getting **false**. Checked with Get requests but not seeing the latest file uploaded.  
let me know if any more information required.

---

<div class="post-metadata">

**Author:** ![mikahanninen](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/mikahanninen/32/62_2.png) [@mikahanninen](https://forum.robotframework.org/u/mikahanninen)\
**Post date:** [23 May 2020 08:57 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/9 "2020-05-23T08:57:27Z")

</div>

Have you tried with **files** parameter ? @SuperSonic

```
${file_data}= Get Binary File ${CURDIR}${/}data.json
&{files}= Create Dictionary file=${file_data}
${resp}= Post Request mysession /api/insertFile files=${files}
```

---

<div class="post-metadata">

**Author:** ![SuperSonic](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@SuperSonic](https://forum.robotframework.org/u/SuperSonic)\
**Post date:** [23 May 2020 14:53 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/10 "2020-05-23T14:53:28Z")

</div>

Yes, I have tried this before, Get Binary File keyword copies data from the file. I want to upload any specific file it could be .txt file, .docx file, .xls file, .pdf file on the server by using file upload API. Posting details of API from swagger this time hope it will help us to find the issue.!!

 ![API1](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/1X/1b6d892b667fae76bcb957d9a406e651fe761eba.png)

---

<div class="post-metadata">

**Author:** ![SuperSonic](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@SuperSonic](https://forum.robotframework.org/u/SuperSonic)\
**Post date:** [23 May 2020 14:53 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/11 "2020-05-23T14:53:59Z")

</div>

Here is the successful one

 ![Success](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/1X/79cc2c0e00e50ae5d47126bc26a1b00fc13782cd.png)

---

<div class="post-metadata">

**Author:** ![SuperSonic](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@SuperSonic](https://forum.robotframework.org/u/SuperSonic)\
**Post date:** [26 May 2020 11:57 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/12 "2020-05-26T11:57:56Z")

</div>

I tried every possible way to fix this issue but no luck. 🙁

---

<div class="post-metadata">

**Author:** ![SuperSonic](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@SuperSonic](https://forum.robotframework.org/u/SuperSonic)\
**Post date:** [3 June 2020 14:13 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/13 "2020-06-03T14:13:19Z")

</div>

Any solution?

---

<div class="post-metadata">

**Author:** ![SuperSonic](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@SuperSonic](https://forum.robotframework.org/u/SuperSonic)\
**Post date:** [18 June 2020 07:43 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/14 "2020-06-18T07:43:34Z")

</div>

Any lead on this issue will be appreciated.  
Thanks

---

<div class="post-metadata">

**Author:** ![René](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/ren%C3%A9/32/7_2.png) [@René](https://forum.robotframework.org/u/Ren%C3%A9)\
**Post date:** [18 June 2020 11:03 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/15 "2020-06-18T11:03:05Z")

</div>

i am very sorry that i can not help you with this answer.  
You can also try to ask in Slack…

[https://robotframework.org/#community](https://robotframework.org/#community)

---

<div class="post-metadata">

**Author:** ![slnecnik](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/slnecnik/32/740_2.png) [@slnecnik](https://forum.robotframework.org/u/slnecnik)\
**Post date:** [6 August 2020 06:12 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/16 "2020-08-06T06:12:02Z")

</div>

For tmp workaround try using KW Run from Operating System library. And use curl command copied from swagger / exported from Postman.

---

<div class="post-metadata">

**Author:** ![lucagiove](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/lucagiove/32/85_2.png) [@lucagiove](https://forum.robotframework.org/u/lucagiove)\
**Post date:** [4 June 2021 20:56 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/17 "2021-06-04T20:56:40Z")

</div>

I think I found the issue, you were passing the content-type header while requests will handle it automatically adding the boundary.

Here an example: [robotframework-requests/test\_post\_multipart.robot at master · MarketSquare/robotframework-requests · GitHub](https://github.com/MarketSquare/robotframework-requests/blob/master/atests/test_post_multipart.robot)

And more info here: [Multiple fields in one multipart/form-data request part · Issue #218 · MarketSquare/robotframework-requests · GitHub](https://github.com/MarketSquare/robotframework-requests/issues/218)

---

<div class="post-metadata">

**Author:** ![automat](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/automat/32/2973_2.png) [@automat](https://forum.robotframework.org/u/automat)\
**Post date:** [6 September 2023 21:21 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/18 "2023-09-06T21:21:18Z")

</div>

Hello, I have completely the same issue as described here. I tried all ways mentioned here but still struggling with finding solution.  
SuperSonic: Have you managed to solve it? Thanks

---

<div class="post-metadata">

**Author:** ![thangtcq](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/thangtcq/32/2591_2.png) [@thangtcq](https://forum.robotframework.org/u/thangtcq)\
**Post date:** [24 February 2025 10:14 UTC](https://forum.robotframework.org/t/facing-issue-with-testing-file-upload-api-using-robot-framework/254/19 "2025-02-24T10:14:39Z")

</div>

I got it, this is my code.

```python
User Profile
    [Arguments] ${req_token} ${req_fullname} ${req_birthday} ${req_avatar} ${req_captcha}=req_captcha
    ${image}= Evaluate {'avatar': ('avatar.png', open('${EXECDIR}/ResourceProvider/avatar.png', 'rb'), 'image/jpeg')}
    &{body} Create Dictionary full_name=${req_fullname} birthday=${req_birthday} captcha=${req_captcha}
    &{headers} Create Dictionary Authorization=Token ${req_token}
    ${resp} POST On Session session /api/v1/user files=${image} data=${body} headers=${headers}

```
