jnunemaker / jnunemaker/httparty
Question: Formatting an array of objects for multipart form-data
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 5.9k
- Forks
- 973
- Avg merge
- 1h 59m
- Merged PRs (30d)
- 1
Description
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?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with HashConversions.normalize_keys(key, value) and generate_multiform, then reproduce the nested array-of-hashes request using the Ruby 2.7.0 and HTTParty 0.20.0 example. Trace how normalized entries become multipart pairs and compare the generated body with the expected fields. Done means the behavior is either corrected with coverage for multiple hash entries or clearly documented as unsupported.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100