chopper icon indicating copy to clipboard operation
chopper copied to clipboard

Query parameters with Map values are not properly encoded

Open point-source opened this issue 3 years ago • 1 comments

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.

point-source avatar Aug 10 '21 01:08 point-source

You could simply encode that map into a JSON string using json.encode() and pass it as a String.

techouse avatar Oct 05 '22 12:10 techouse

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.

point-source avatar Mar 11 '23 21:03 point-source

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 avatar Mar 11 '23 21:03 point-source

@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.

techouse avatar Mar 11 '23 21:03 techouse

Great to know, thanks!

point-source avatar Mar 12 '23 19:03 point-source