Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Formatting an array of objects for multipart form-data #757

Open
mvalerie23 opened this issue Jul 25, 2022 · 0 comments
Open

Question: Formatting an array of objects for multipart form-data #757

mvalerie23 opened this issue Jul 25, 2022 · 0 comments

Comments

@mvalerie23
Copy link

Ruby 2.7.0
HTTParty 0.20.0

I am having trouble correctly generating an array of hashes for form-data. The first key-value in my array is correctly added but subsequent key-values are truncated out

Example request body:

HTTParty.post(
       https://example.com,
        headers: { Authorization: "Bearer #{token}" },

        body: {
            request: {
            description:description,
            information: {
                  name: "name",
                 data: [
                  { phone: "123-456-7890",
                    email: "[email protected]",
                    method: "email"},
                  { phone: "098-765-4321",
                    email: "[email protected]",
                    method: "phone"},
                  ]
            },
            documents: {
                data:  File.open('test/test/test/document.txt')
            }
          }
        }
      )

After some digging, I saw that HashConversions.normalize_keys(key, value) formatted the data array objects to be inside a single array

[["request[description]", "ac4ce6c6-1e86-4dbc-a22d-b8d83a12bd39"], 
["request[information][name]", "79ab235d-2ca3-4a46-a76e-fc9c655667af"], 
["request[information][data][][phone]", "123-456-7890.", 
"request[information][data][][email]", "[email protected]", 
"request[information][data][][method]", "email"], 
["request[information][data][][phone]", "098-765-4321.", 
"request[information][data][][email]", "[email protected]", 
"request[information][data][][method]", "phone"]]

When generate_multiform parses through the arrays, it only takes the first two values as a pair and ignores the rest, resulting in only the following being added to the body

form-data; name=\"request[information][data][][phone]\"\r\n\r\123-456-7890\r\n--------------------------v3xmYTKfCUOOeMXt\r\nContent-Disposition: 
form-data; name=\"request[information][data][][phone]\"\r\n\r\n098-765-4321\r\n--------------------------v3xmYTKfCUOOeMXt--\r\n"

A not ideal workaround has been to have each key value in an array, but that also gives an incorrect format

HTTParty.post(
        URL,
        headers: { Authorization: "Bearer #{token}" },

        body: {
            request: {
            description:description,
            information: {
                  name: "name",
                 data: [
                     [{ phone: "123-456-7890"}],
                     [{email: "[email protected]"}],
                     [{method:"email"]
                 ]  
            },
            documents: {
                data:  File.open('test/test/test/document.txt')
            }
          }
        }
      )
form-data; name=\"request[information][data][][][phone]\"\r\n\r\n123-456-7890\r\n--------------------------jpF9PvkiLwgdd61g\r\nContent-Disposition: 
form-data; name=\"request[information][data][][][email]\"\r\n\r\[email protected]\r\n--------------------------jpF9PvkiLwgdd61g\r\nContent-Disposition: 
form-data; name=\"request[information][data][][][method]\"\r\n\r\nemail\r\n--------------------------jpF9PvkiLwgdd61g\r\nContent-Disposition: 

I was wondering if someone could advise on how to generate form data for the scenario above, or if it is not something currently supported?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant