AJAX api accepting list of dictionaries as input parameter

What I’m trying to do:
I’m trying to create an API server, with an endpoint that can accept a list of dictionaries.

What I’ve tried and what’s not working:
I’ve tried using @anvil.server.http_endpoint, which was working well for simple input types (string, numbers, etc.), but is failing when I try to send a list of dictionaries.

Code Sample:

@anvil.server.http_endpoint('/v1/check')
def v1_check(english_translation='', english_sentence='', possible_mistakes=None):
   """
   I need possible_mistakes to accept a list of dictionaries.
   """

Workaround:
For now, I changed possible_mistakes to accept a string, and then on the server-side, I’m decode using json.loads(possible_mistakes) to convert it to a list of dictionaries.

But this feels like a hack… Is there a better way to do this?

I think that an http endpoint should be able to manage one json object, I don’t think it can manage multiple arguments and one of them is a json object.

I’ve also had bad experience with that automatic json object management that was working with a client and not with another.

So, rather than debugging al the clients in whatever platform and language they are, I started making my Anvil end points like this and they never fail:

  bb = anvil.server.request.body.get_bytes()
  data = json.loads(bb)

The client packs the parameters into a dictionary like this:

{
  'english_translation': english_translation,
  'english_sentence': english_sentence, 
  'possible_mistakes': None,
}

Se also: Http_endpoint [POST] Issues Publishing to Data Table - #4 by stefano.menci

1 Like