# Api request - form data body

**URL:** <https://forum.robotframework.org/t/api-request-form-data-body/5597>\
**Category:** RequestsLibrary\
**Created:** [23 April 2023 12:18 UTC](https://forum.robotframework.org/t/api-request-form-data-body/5597 "2023-04-23T12:18:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![KGhonim](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/kghonim/32/2442_2.png) [@KGhonim](https://forum.robotframework.org/u/KGhonim)\
**Post date:** [23 April 2023 12:18 UTC](https://forum.robotframework.org/t/api-request-form-data-body/5597/1 "2023-04-23T12:18:18Z")

</div>

i need your support please , i am trying to write test case for api request in a data-driven style as below details  
data file is .csv  
request body : form data where keys ( entity , content )  
entity key is atext and content is a file  
header contain user token  
below postman screen shot

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

the error a post request body but i can’t figure out how to fix

Code snippet  
\*\*\* Settings \*\*\*  
Library RequestsLibrary  
Library JSONLibrary  
Library Collections  
Library OperatingSystem  
Library CSVLibrary  
Library String  
Library DateTime

Resource ./Environment.resource

\*\*\* Keywords \*\*\*  
Login  
[Arguments] ${filePath}  
Create Session APP\_Login ${Serverurl}  
${jsonBody}= Load Json From File ${EXECDIR}${filePath}  
Set To Dictionary ${jsonBody} username=${Username} password=${Password} app=${Entity}  
${response}= POST On Session APP\_Login ${Login} json=${jsonBody}  
# Log To Console ${response.content}  
${token}= Set Variable ${response.json() [‘rs’]}  
set Suite variable ${token}  
${app-auth-header}= Create Dictionary app-auth-header=${token}  
Create Session app\_User ${Serverurl} headers=${app-auth-header}  
##### assertions #####  
Should Be Equal As Strings ${response.status\_code} 200

################# Incoming ################  
IN Entity  
[Arguments] ${row}  
${entity}= Create Dictionary docSubject=${row[0]} addMethod=1 docNotes=null docStatus=4 docDate=${row[1]} docType=null fileCode=null fileSerial=null fileId=null mainClassification=null subClassification=null ou=${row[2]} registryOU=${row[3]} securityLevel=1 priorityLevel=0 refDocNumber=${row[4]} mainSiteId=${row[5]} subSiteId=${row[6]} refDocDate=${row[7]} followupStatus=1 followupDate=null siteType=3  
[Return] ${entity}

IN Content  
[Arguments] ${row}  
${content}= Create Dictionary content=${row[8]}  
${files}= Create List content=${row[8]}  
[Return] ${content}

Get File Name  
[Arguments] ${file\_path}  
${path\_components}= Split String ${file\_path} /  
${file\_name}= Get From List ${path\_components} -1  
[Return] ${file\_name}

Add and Archive  
[Arguments] ${file\_path}  
${records}= Read Csv File To List ${EXECDIR}${file\_path} delimiter=,  
FOR ${row} IN @{records}  
${entity}= IN Entity ${row}  
Log To Console the entity body : ${entity}

```
    ${content}= IN Content ${row}
    Log To Console the Content body : ${content}

    ${boundary}= Generate Random String length=10 chars=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
    ${headers}= Create Dictionary Content-Type=multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

    ${data}= Create Dictionary entity=${entity} content=${content}
    Log To Console the request body : ${data}

    ${content_exists}= Run Keyword And Return Status Dictionary Should Contain Key ${data} content
    Set Variable If ${content_exists} ${data["content"]} ${content}

    ${entity_exists}= Run Keyword And Return Status Dictionary Should Contain Key ${data} entity
    Set Variable If ${entity_exists} ${data["entity"]} ${entity}
    Log To Console the request body : ${data}
    run keyword if ${content_exists} Set Variable ${data["content"]} ${content}
    run keyword if ${entity_exists} Set Variable ${data["entity"]} ${entity}
    ${data}= Evaluate {'entity':(None,${entity}) ,'content': (None,${content})}
    Log To Console the request body : ${data}

    ${response}= POST On Session app_User ${endpoint} headers=${headers} data=${data}

    # ${docId}= Set Variable ${response.json()['rs']}
    # Log Added incoming correspondence with doc ID ${docId}
    # ${archive_response}= POST On Session app_User /url/${docId}/archive
    # Log ID ${docId}
    BREAK
END

```

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

login  
[Tags] Login  
Login “jsonfilepath”

Add  
[Tags]  
Add and Archive “csvfilepath”

after run all keyword passed but i have 500 error at post step

KEYWORD ${response} = RequestsLibrary . POST On Session app\_User , ${endpoint}, headers=${headers}, data=${data}  
Documentation:   
Sends a POST request on a previously created HTTP Session.

Start / End / Elapsed: 20230423 15:08:17.809 / 20230423 15:08:18.272 / 00:00:00.463  
15:08:18.159 INFO POST Request : url=full url  
path\_url=${endpoint}  
headers={‘User-Agent’: ‘python-requests/2.28.2’, ‘Accept-Encoding’: ‘gzip, deflate’, ‘Accept’: ‘_/_’, ‘Connection’: ‘keep-alive’, ‘app-auth-header’: ‘token’, ‘Content-Type’: ‘multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW’, ‘Content-Length’: ‘413’}  
body=entity=docSubject&entity=addMethod&entity=docNotes&entity=docStatus&entity=docDate&entity=docType&entity=fileCode&entity=fileSerial&entity=fileId&entity=mainClassification&entity=subClassification&entity=ou&entity=registryOU&entity=securityLevel&entity=priorityLevel&entity=refDocNumber&entity=mainSiteId&entity=subSiteId&entity=refDocDate&entity=followupStatus&entity=followupDate&entity=siteType&content=content

15:08:18.160 INFO POST Response : fullurl  
status=500, reason=Internal Server Error  
headers={‘Vary’: ‘Origin, Access-Control-Request-Method, Access-Control-Request-Headers’, ‘Content-Type’: ‘application/json’, ‘X-Content-Type-Options’: ‘nosniff’, ‘X-XSS-Protection’: ‘1; mode=block’, ‘Cache-Control’: ‘no-cache, no-store, max-age=0, must-revalidate’, ‘Pragma’: ‘no-cache’, ‘Expires’: ‘0’, ‘X-Frame-Options’: ‘DENY’, ‘Content-Language’: ‘en-US’, ‘Transfer-Encoding’: ‘chunked’, ‘Connection’: ‘Close’, ‘Date’: ‘Sun, 23 Apr 2023 12:08:16 GMT’}  
body={“sc”:500,“ms”:“Required request parameter ‘entity’ for method parameter type String is not present”,“ec”:12000,“lm”:[“2023-04-23 15:08:17,926 INFO [WebContainer : 13] c.e.c.s.security.LoggingInterceptor.preHandle [31] - Starting Handling Request Number 5005400083053282480”,“2023-04-23 15:08:17,926 ERROR [WebContainer : 13] c.e.c.s.e.ExceptionControllerAdvice.exception [72] - Undefined Exception {}”,“org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter ‘entity’ for method parameter type String is not present”

---

<div class="post-metadata">

**Author:** ![KGhonim](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/kghonim/32/2442_2.png) [@KGhonim](https://forum.robotframework.org/u/KGhonim)\
**Post date:** [24 April 2023 23:29 UTC](https://forum.robotframework.org/t/api-request-form-data-body/5597/2 "2023-04-24T23:29:52Z")

</div>

any help ?

---

<div class="post-metadata">

**Author:** ![robinmackaij](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/robinmackaij/32/126_2.png) [@robinmackaij](https://forum.robotframework.org/u/robinmackaij)\
**Post date:** [26 April 2023 16:39 UTC](https://forum.robotframework.org/t/api-request-form-data-body/5597/3 "2023-04-26T16:39:35Z")

</div>

The `${data}` variable you create is not a dict, as can be seen from the log (INFO POST Request).

Due to reuse of variable names, I can’t really indicate where it might go off-track, but the Evaluate expression is suspect. Within RF scope, I’d suggest to use the keywords from the Collections library to manipulate lists or dicts.

---

<div class="post-metadata">

**Author:** ![zaynab](https://avatars.discourse-cdn.com/v4/letter/z/b9bd4f/32.png) [@zaynab](https://forum.robotframework.org/u/zaynab)\
**Post date:** [8 October 2023 12:50 UTC](https://forum.robotframework.org/t/api-request-form-data-body/5597/4 "2023-10-08T12:50:03Z")

</div>

still having the problem ,could you explain the anser if you knew ?
