# How to post a request with form-data using Robotframework

**URL:** <https://forum.robotframework.org/t/how-to-post-a-request-with-form-data-using-robotframework/7749>\
**Category:** Robot Framework\
**Created:** [30 August 2024 06:36 UTC](https://forum.robotframework.org/t/how-to-post-a-request-with-form-data-using-robotframework/7749 "2024-08-30T06:36:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikhileshmitra](https://avatars.discourse-cdn.com/v4/letter/n/a6a055/32.png) [@nikhileshmitra](https://forum.robotframework.org/u/nikhileshmitra)\
**Post date:** [30 August 2024 06:36 UTC](https://forum.robotframework.org/t/how-to-post-a-request-with-form-data-using-robotframework/7749/1 "2024-08-30T06:36:03Z")

</div>

curl -X ‘POST’   
'[https://localhost:8443/ABCD/9/XYZ?operation=CREATE&reason=10’](https://localhost:8443/ABCD/9/XYZ?operation=CREATE&reason=10%27)   
-H ‘accept: application/json’   
-H ‘Content-Type: multipart/form-data’   
-F ‘file=@bulk\_insert\_sample.txt;type=text/plain’

I tried this →

```
Create Session bulk_download ${BASE_URL} verify=False
${headers}= Create Dictionary Authorization=Bearer ${ACCESS_TOKEN} accept=application/json
${file_content}= Get File ${FILE_PATH}
&{multipart_file}= Create Dictionary file=( ${FILE_NAME}, ${file_content}, ${CONTENT_TYPE})
${response}= POST On Session bulk_download ${ENDPOINT_CREATE} headers=${headers} files=${multipart_file}
Should Be Equal As Numbers ${response.status_code} 200

```

But unfortunately getting , file.getContentType()= null from BE, though the file content and path is valid

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [30 August 2024 09:55 UTC](https://forum.robotframework.org/t/how-to-post-a-request-with-form-data-using-robotframework/7749/2 "2024-08-30T09:55:36Z")

</div>

Hi Nikhilesh,

A couple of things I notice right away:

- The line starting `${headers}= Create Dictionary ` is missing the `Content-Type:` header
- The line starting `&{multipart_file}= Create Dictionary`, `${CONTENT_TYPE}` should be the file’s content type (e.g. `text/plain`) not the `multipart/form-data` content type

> where `'content-type'` is a string defining the content type of the given file

Hopefully that’s all you need 🤞

Dave.

---

<div class="post-metadata">

**Author:** ![nikhileshmitra](https://avatars.discourse-cdn.com/v4/letter/n/a6a055/32.png) [@nikhileshmitra](https://forum.robotframework.org/u/nikhileshmitra)\
**Post date:** [1 September 2024 07:52 UTC](https://forum.robotframework.org/t/how-to-post-a-request-with-form-data-using-robotframework/7749/3 "2024-09-01T07:52:43Z")

</div>

> [@damies13](#):
>
> text/plain

curl -X ‘POST’   
'[https://localhost:8443/ABCD/9/XYZ?operation=CREATE&reason=10’](https://localhost:8443/ABCD/9/XYZ?operation=CREATE&reason=10%27)   
-H ‘accept: application/json’   
-H ‘Content-Type: multipart/form-data’   
-F ‘file=@bulk\_insert\_sample.txt;type=text/plain’

this culr command was working

But

\*\*\* Settings \*\*\*  
Library RequestsLibrary  
Library BuiltIn  
Library String  
Library OperatingSystem

\*\*\* Variables \*\*\*  
${BASE\_URL} [https://localhost:8003/](https://localhost:8003/)  
${ACCESS\_TOKEN} eyJhbGciOiNiJ9.eyJzdWIiOiEwMH0.pMIZh-HxHFrdHmKMjg  
${ENDPOINT\_CREATE} /eq/9/optxyz?opt=CREATE&rn=10  
${FILE\_PATH} C:/\*\*\*\*\*\*\*/bulkinsert.txt

\*\*\* Test Cases \*\*\*  
Test Upload Success  
[Documentation] Test case that simulates different download scenarios  
Create Session Session\_download ${BASE\_URL} verify=False  
${headers}= Create Dictionary Authorization=Bearer ${ACCESS\_TOKEN} accept=application/json Content-Type=Multipart/form-data  
${file\_content}= Get File ${FILE\_PATH}  
&{multipart\_file}= Create Dictionary file=( test.txt, ${FILE\_CONTENT}, text/plain)  
${response}= POST On Session Session\_download ${ENDPOINT\_CREATE} headers=${headers} files=${multipart\_file}  
Should Be Equal As Numbers ${response.status\_code} 200

is returning

2024-09-01 03:49:56.799 9010c85a-0160-409a-95dc-9b3b22fc0a47 ERROR - org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request

“INTERNAL\_SERVER\_ERROR”,“description”:“Failed to parse multipart servlet request”

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [1 September 2024 08:12 UTC](https://forum.robotframework.org/t/how-to-post-a-request-with-form-data-using-robotframework/7749/4 "2024-09-01T08:12:32Z")

</div>

Hi Nikhilesh,

I think you need to change this line:

```python
${file_content}= Get File ${FILE_PATH}

```

with this:

```python
${file_content}= Get File For Streaming Upload ${FILE_PATH}

```

Also FYI watch your variable names `${file_content}` is not the same as `${FILE_CONTENT}`

Dave.

---

<div class="post-metadata">

**Author:** ![nikhileshmitra](https://avatars.discourse-cdn.com/v4/letter/n/a6a055/32.png) [@nikhileshmitra](https://forum.robotframework.org/u/nikhileshmitra)\
**Post date:** [1 September 2024 08:27 UTC](https://forum.robotframework.org/t/how-to-post-a-request-with-form-data-using-robotframework/7749/5 "2024-09-01T08:27:19Z")

</div>

I change as per your suggestion but the error is same

2024-09-01 04:20:36.723 15a2bd3e-0f15-42d9-8a74-98023c25c550 ERROR - org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/e/ee0ee3b48d984df39cb3296c4a25018244e1a63e.png)

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [2 September 2024 14:23 UTC](https://forum.robotframework.org/t/how-to-post-a-request-with-form-data-using-robotframework/7749/6 "2024-09-02T14:23:35Z")

</div>

Hi Nikhilesh,

I wasn’t sure where to guide you from here, but after thinking on it a bit, I would try running the curl command with a -v switch and saving the request headers and request body from that command and compare it to what you have from requests library

Look at the content-length header and make sure they are the same size when uploading the same file

Then check the other headers and see if any are missing or extra.

Dave.

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [2 September 2024 14:30 UTC](https://forum.robotframework.org/t/how-to-post-a-request-with-form-data-using-robotframework/7749/7 "2024-09-02T14:30:10Z")

</div>

Hi Nikhilesh,

I also noticed these line breaks:

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/c/c461e7a15eaae9ab8873307cb69537e07f7fcf49.png)

They looked a little odd to me, which made me look close and notice the & on &{multipart\_files} on the `POST On Session` line, I think on that line it should be a $ not &, so you could try that too.

Dave.

---

<div class="post-metadata">

**Author:** ![nikhileshmitra](https://avatars.discourse-cdn.com/v4/letter/n/a6a055/32.png) [@nikhileshmitra](https://forum.robotframework.org/u/nikhileshmitra)\
**Post date:** [12 September 2024 03:52 UTC](https://forum.robotframework.org/t/how-to-post-a-request-with-form-data-using-robotframework/7749/8 "2024-09-12T03:52:58Z")

</div>

> [@damies13](#):
>
> `${file_content}= Get File For Streaming Upload ${FILE_PATH}`

I tried with a different way →

File Valid  
${headers}= Create Dictionary Authorization=Bearer ${ACCESS\_TOKEN} accept=application/json Content-Type=multipart/form-data  
Create Session bulk\_upload ${BASE\_URL} verify=False  
${response}= POST On Session bulk\_upload ${ENDPOINT\_BULK\_UPLOAD} headers=${headers} files={“file”: (“bulkinsert.txt”, open(“${FILE\_PATH}”, “rb”), “${CONTENT\_TYPE}”)}  
Should Be Equal As Numbers ${response.status\_code} 200

Now getting this error\_\_\>

${response} = RequestsLibrary . POST On Session bulk\_upload ${ENDPOINT\_BULK\_UPLOAD} headers=${headers} files={“file”: (“bulkinsert.txt”, open(“${FILE\_PATH}”, “rb”), “${CONTENT\_TYPE}”)}

| Documentation: | Sends a POST request on a previously created HTTP Session. |
| --- | --- |
| Start / End / Elapsed: | 20240912 09:21:52.465 / 20240912 09:21:52.474 / 00:00:00.009 |

09:21:52.468 FAIL ValueError: cannot encode objects that are not 2-tuples

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [12 September 2024 13:07 UTC](https://forum.robotframework.org/t/how-to-post-a-request-with-form-data-using-robotframework/7749/9 "2024-09-12T13:07:31Z")

</div>

Hi Nikhilesh,

At least that error is telling you what’s wrong:  
This tuple is a 3-tuple not a 2-tuple

```python
(“bulkinsert.txt”, open(“${FILE_PATH}”, “rb”), “${CONTENT_TYPE}”)

```

try changing it to:

```python
(“bulkinsert.txt”, open(“${FILE_PATH}”, “rb”))

```

I know the documentation said it supports 3-tuple and 4-tuples, but perhaps you found a bug in bulk\_upload or the documentation?

Dave.

---

<div class="post-metadata">

**Author:** ![nikhileshmitra](https://avatars.discourse-cdn.com/v4/letter/n/a6a055/32.png) [@nikhileshmitra](https://forum.robotframework.org/u/nikhileshmitra)\
**Post date:** [13 September 2024 06:04 UTC](https://forum.robotframework.org/t/how-to-post-a-request-with-form-data-using-robotframework/7749/10 "2024-09-13T06:04:47Z")

</div>

I am able to fix the issue. I use this

File Valid  
${headers}= Create Dictionary Authorization=Bearer ${ACCESS\_TOKEN} accept=application/json  
Create Session bulk\_upload ${BASE\_URL} verify=False  
${file\_content}= Get File ${FILE\_PATH}  
${file\_tuple}= Create List bulkinsert.txt ${file\_content} ${CONTENT\_TYPE}  
${file}= Create Dictionary file=${file\_tuple}  
${response}= POST On Session bulk\_upload ${ENDPOINT\_BULK\_UPLOAD} headers=${headers} files=${file}  
Should Be Equal As Numbers ${response.status\_code} 200

Thank you for your kind support 🙂
