chopper
chopper copied to clipboard
Query parameters with Map values are not properly encoded
Using chopper 4.0.1, I am trying to encode a query param called "filter" which accepts a map with this structure:
{
"Field": "QuoteNumber",
"Op": "=",
"Value": "306142"
}
The correct encoding of this is:
filter=%7B%0A%20%20%22Field%22%3A%20%22QuoteNumber%22%2C%0A%20%20%22Op%22%3A%20%22%3D%22%2C%0A%20%20%22Value%22%3A%20%22306142%22%0A%7D
But chopper encodes it as:
filter=%7BField%3A+QuoteNumber%2C+Op%3A+%3D%2C+Value%3A+306142%7D
which gets decoded to:
{Field:+QuoteNumber,+Op:+=,+Value:+306142}
This is obviously not valid json for a number of reasons and as a result, the server throws a 400 or 500 error.
You could simply encode that map into a JSON string using json.encode()
and pass it as a String
.
True. But this is happening within a library generator that uses chopper as a dependency. I'd rather see it fixed than convince the author of the generator(s) to work around it.
Furthermore, if formatting of the value is required prior to passing it to chopper, then chopper should specify the type it expects (such as string) or throw when an unencodable type is used.
@point-source This should have been fixed in this PR already https://github.com/lejard-h/chopper/pull/364
{
"Field": "QuoteNumber",
"Op": "=",
"Value": "306142"
}
would get encoded as
hxxp://path.to/api/?filter.Field=QuoteNumber&filter.Op=%3D&filter.Value=306142
Check the PR comments for more examples.
Great to know, thanks!