Post on Session with multipart/form-data and different contentType

324 views
Skip to first unread message

Jozsef Roka

unread,
Jan 17, 2023, 9:43:42 AM1/17/23
to robotframework-users
Hi All,

Please help me, I have no more ideas.
I woud like create a request payload like this:

------WebKitFormBoundaryy7eUrxsZTzwEYAZB
Content-Disposition: form-data; name="__JsonData__"

{"postedFile":{"fileName":""},"GroupId":"fa1b569c-cbff-e911-80c6-14dae9ebd5bf"}
------WebKitFormBoundaryy7eUrxsZTzwEYAZB
Content-Disposition: form-data; name="postedFile"; filename="import_test.csv"
Content-Type: text/csv


------WebKitFormBoundaryy7eUrxsZTzwEYAZB--


Or this (FormData):
__JsonData__: {"postedFile":{"fileName":""},"
GroupId":"fa1b569c-cbff-e911-80c6-14dae9ebd5bf"}
postedFile: 
(binary)

I read these docs:

HTTP Request - CreateFile Upload
   HTTP Request - Create Session    ${USERNAME}    ${PASS}   /Import    
   
    &{fileParts}=  Create Dictionary
    ${data}=    Set Variable    {"postedFile":{"fileName":""},"GroupId":"${suite_id}"}
    Create Multi Part    ${fileParts}    __JsonData__    ${EMPTY}    contentType=application/json    content=${data}    
    Create Multi Part   ${fileParts}    postedFile    ${EXECDIR}${/}DATA${/}import_test.csv    contentType=text/csv
   
    ${response}=    POST On Session   login_session     /Import    files=${fileParts}

    Check Response    ${response}

Create Multi Part
    [Arguments]    ${addToDictionary}    ${partName}    ${filePath}=${EMPTY}    ${contentType}=${EMPTY}    ${content}=${EMPTY}
    ${fileData}=    Run Keyword If    '${content}' != '${EMPTY}'    Set Variable    ${content}
    ...            ELSE    Get File For Streaming Upload    ${filePath}
    ${fileDir}    ${fileName}=    Split Path    ${filePath}
   
    ${partData}=    Create List    ${fileName}    ${fileData}    ${contentType}

    Set To Dictionary    ${addToDictionary}    ${partName}=${partData}

And I saw this in the log (request body):

body=b'--c65d76365c55397ff595ada7ee91c2fa\r\nContent-Disposition: form-data; name="__JsonData__"; filename="."\r\nContent-Type: application/json\r\n\r\n{"postedFile":{"fileName":""},"GroupId":"ad4446cd-cbff-e911-80c6-14dae9ebd5bf"}\r\n--c65d76365c55397ff595ada7ee91c2fa\r\nContent-Disposition: form-data; name="postedFile"; filename="import_test.csv"\r\nContent-Type: text/csv\r\n\r\n826774;Brian Stewart;20.0\r\nTotal;1;20.0\r\n--c65d76365c55397ff595ada7ee91c2fa--\r\n' 

And the response:

body={"type":"RuLeRErrorModel","errorModel":{"GroupId":[{"message":{"identifier":null,"text":"The group field is required."},"fields":["GroupId"]}]},"isErrorResponse":true} 

Do you have any ideas on where the error could be?
Thanks,
József Róka
Reply all
Reply to author
Forward
0 new messages