1

I am facing very strange issue in Flask and flask-rest-jsonapi spec.

If I send below url using postman i am getting very weird behavior :-

Not working URL:-

http://127.0.0.1:5000/v1/recordings?filter=[
  {
    "name": "rtags",
    "op": "any",
    "val": {
      "name": "tag",
      "op": "ilike",
      "val": "%car%"
    }
  }
]

In encoded url ca is replaced by different characters %EF%BF%BD .

http://127.0.0.1:5000/v1/recordings?filter=%5B%0A++%7B%0A++++%22name%22%3A+%22rtags%22%2C%0A++++%22op%22%3A+%22any%22%2C%0A++++%22val%22%3A+%7B%0A++++++%22name%22%3A+%22tag%22%2C%0A++++++%22op%22%3A+%22ilike%22%2C%0A++++++%22val%22%3A+%22**%EF%BF%BD**r%25%22%0A++++%7D%0A++%7D%0A%5D

However if i send instead of car some different word like traffic then encoded url string is fine.

Working URL :-

http://127.0.0.1:5000/v1/recordings?filter=[
  {
    "name": "rtags",
    "op": "any",
    "val": {
      "name": "tag",
      "op": "ilike",
      "val": "%traffic%"
    }
  }
] 

e.g.

"http://127.0.0.1:5000/v1/recordings?filter=%5B%0A++%7B%0A++++%22name%22%3A+%22rtags%22%2C%0A++++%22op%22%3A+%22any%22%2C%0A++++%22val%22%3A+%7B%0A++++++%22name%22%3A+%22tag%22%2C%0A++++++%22op%22%3A+%22ilike%22%2C%0A++++++%22val%22%3A+%22%25**traffic**%25%22%0A++++%7D%0A++%7D%0A%5D"

Also if i put the breakpoint in app.py Flask file i get first two characters as capital "CAr" instead of "car" which i have send in query-string parameter.

<Request 'http://127.0.0.1:5000/v1/recordings?filter=%5B%0A%20%20%7B%0A%20%20%20%20%22name%22:%20%22rtags%22,%0A%20%20%20%20%22op%22:%20%22any%22,%0A%20%20%20%20%22val%22:%20%7B%0A%20%20%20%20%20%20%22name%22:%20%22tag%22,%0A%20%20%20%20%20%20%22op%22:%20%22ilike%22,%0A%20%20%20%20%20%20%22val%22:%20%22%CAr%25%22%0A%20%20%20%20%7D%0A%20%20%7D%0A%5D' [GET]>}
jelhan
  • 6,149
  • 1
  • 19
  • 35
nja
  • 67
  • 2
  • 13
  • You need to close the array with ] in Not working URL. http://127.0.0.1:5000/v1/recordings?filter=[ { "name": "rtags", "op": "any", "val": { "name": "tag", "op": "ilike", "val": "%car%" } }] – ASSILI Taher Oct 07 '19 at 21:19
  • Thanks for pointing out but sorry it was typo which i have corrected . I had closed the array but same issue. – nja Oct 07 '19 at 23:20
  • Reproducing the issue: `>>> from urllib.parse import unquote; unquote("%ca").encode("utf8")` --> `b'\xef\xbf\xbd'`. I assume you need to escape the "%" somehow to prevent "%ca" from getting interpreted as URL-encoded characters. – jDo Oct 07 '19 at 23:28
  • Thanks but i am not able to understand why in case of other sequence like%traffic it is fine . What is so unique about %ca ? – nja Oct 07 '19 at 23:35
  • The issue only affects characters that could be [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) (A-F, 0-9) which is why it works for "%traffic%" but not for "%ca". In case you always get the string "%EF%BF%BD" no matter the input, it's because you're actually seeing the UTF-8 encoded representation of the [unicode replacement character](https://en.wikipedia.org/wiki/Replacement_character) "�" which is often used to indicate an encoding/decoding error. – jDo Oct 07 '19 at 23:43
  • 1
    Maybe it'll work if you replace the "%" in your string with its URL-encoded version "%25" so "%car%" becomes "%25car%25". – jDo Oct 07 '19 at 23:51
  • 1
    Thanks once again and it is working fine if i replace % with %25 in query-string however this is not mentioned in any of documentation . I assume that internally Flask shall take care of this and not application has to do this . – nja Oct 08 '19 at 00:02

0 Answers0