jnunemaker / httparty

:tada: Makes http fun again!
MIT License
5.79k stars 968 forks source link

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

Open mvalerie23 opened 2 years ago

mvalerie23 commented 2 years ago

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: "bob@email.com",
                    method: "email"},
                  { phone: "098-765-4321",
                    email: "bob2@email.com",
                    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]", "bob@email.com", 
"request[information][data][][method]", "email"], 
["request[information][data][][phone]", "098-765-4321.", 
"request[information][data][][email]", "bob2@email.com", 
"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: "bob@email.com"}],
                     [{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\nbob@email.com\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?