question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple fields in one multipart/form-data request part

See original GitHub issue

I am trying to use requestslibrary to upload some files, goal is to achieve this:

------WebKitFormBoundary61N9vqJ7380nh6iv
Content-Disposition: form-data; name="files"; filename="photo-2.jpeg"
Content-Type: image/jpeg


------WebKitFormBoundary61N9vqJ7380nh6iv
Content-Disposition: form-data; name="fileId"

b3duLWZpbGVzL2ZmZmZmZmZmYTQyNDVmODAvMjAxNTY*
------WebKitFormBoundary61N9vqJ7380nh6iv
Content-Disposition: form-data; name="extract"

false
------WebKitFormBoundary61N9vqJ7380nh6iv--

and now I have this:

${data}=    Evaluate    {'files': open("C:/testautomation/resources/Assets/photo-2.jpeg", 'r+b'), 'extract': (None, 'false'), 'fileId': (None, 'b3duLWZpbGVzL2ZmZmZmZmZmYTQyNDVmODAvMjAxNTY*')}
${result}=  Post Request  rest  ${url}  headers=${HEADERS}  files=${data}

I THINK that the only bit I am missing is the “Content-Type: image/jpeg” from the first part, but how on earth I can add that? Currently the file gets uploaded, but it is not considered to be a image file.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
emanlovecommented, Mar 4, 2022

@lucagiove what if the multiple parts are not all files?

*** Test Cases ***

Test Post On Session Multipart
    ${file}=  Get File For Streaming Upload  atests/randombytes.bin
    ${multipart_data}=   Create Dictionary  randombytes1  ${file}  author  EdManlove

    ${resp}=    POST On Session  ${GLOBAL_SESSION}   /anything  files=${multipart_data}

Here the requests library assumes both are files, I am assuming because I used the files argument. But if I try with data as in

    ${resp}=    POST On Session  ${GLOBAL_SESSION}   /anything  data=${multipart_data}

it does not seem to know the first is a file. Nor does it seem to build up the multipart data request.

0reactions
webloginwucommented, Oct 24, 2022

I have the same requirement as @emanlove, robotframwork don’t support multiple parts that are not all files. I got the below error: The server cannot or will not process the request, only one file support

Read more comments on GitHub >

github_iconTop Results From Across the Web

multipart/form-data and multiple fields with the same name #737
I'm sending a POST request with multiple fields with the same name which normally works well when passed in a dict: >>> from...
Read more >
HTTP multipart/form-data multiple files in one <input>
According to W3c, multiple files selected in a <input> field, should be send by "multipart/mixed" type with separate boundary string and only ...
Read more >
Create a multipart fom data to send multiple field...
HI, we are struggling with creating a multiple-fileds multipart formdata to send to some APIs. After a lot of frustration with natve policies,...
Read more >
Multipart Request Handling in Spring - Baeldung
Multipart requests consist of sending data of many different types separated by a boundary as part of a single HTTP method call.
Read more >
How to upload file with a form with multiple fields — oracle-tech
With multipart/form-data as the name suggests your form-data contains multiple parts meaning one or more files and some form fields.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found