-
Notifications
You must be signed in to change notification settings - Fork 963
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
HashConversions does not handle objects with varying keys in arrays #719
Comments
FYI, I recently ran into something similar, but I think one of the issues with the post as described here is that you're serializing with The Meanwhile, I.e. with: obj = {
"name" => "Bob",
"phones" => [
{
"type" => "home"
},
{
"type" => "work",
"phone" => "333-333-3333"
}
]
} This works: obj == Rack::Utils.parse_nested_query(HTTParty::HashConversions.to_params(obj))
#=> true And so does this: obj == CGI.parse(URI.encode_www_form(obj))
#=> false, but close (name is now an array and strings are escaped, but home/work aren't mixed up) But if you mix the two, you're going to have a bad time. In other words, if you use All that said, I was just thinking of submitting a pull request that allows passing to HTTParty your own conversion method. |
@JangoSteve thanks for researching that. I'd love a PR. |
+1 here for a PR Running into a similar issue trying to submit requests to a Stripe API endpoint. When sending an array of Because of this, I think that providing one's own conversion method is probably the solution we'd have to take for our use case. |
Using a variant of the example in
HashConversions
, given objectobj
:The following params are produced by
to_params
:Bob's work phone number is now his home number.
I realize working around this would require inspecting all array entries and compiling a superset of all keys.
Perhaps the bigger issue I'm seeing is that the default behavior of
HTTParty.post
whenbody
is an object is to delegate to non-standard serialization of the object, versus assuming the caller wants to send JSON (and thus switch toContent-type: application/json
).Since serialization of array query parameters do not seem to have an agreed-to standard, it would seem more sensical to have this be an opt-in code path, and the default be the JSON code path. Especially when you consider the ambiguous
format: :json
parameter:The combination of
format: :json
and passing of an object to thebody
really kinda makes this request seem like it's going to send a JSON payload. But to the unsuspecting eye, it can inadvertently lead to a lot of Bobs getting their work calls at home.Of course, if you RTFM, you'll understand that
format: :json
only applies to the request, but "Hey, RTFM" sounds like the kind of thing someone might respond to with "You must be fun at parties". And well, this is a HTTParty, right? 😉The text was updated successfully, but these errors were encountered: