Booleans in HttpResponses

What I’m trying to do:
I’m trying to return a json object in a HttpResponse body that contains a boolean value. When executing the request from Postman I’m getting the following error:

Cannot send a Boolean object over HTTP as response[“bool_test”].
You must return either Media, a string, or a JSON-compatible object (lists/dicts/strings/numbers/None).

I would expect booleans to work or am I missing something?

What I’ve tried and what’s not working:

@anvil.server.http_endpoint("/bool_test/",methods=["GET"], cross_site_session=True)
def bool_test(**kwargs):
  return anvil.server.HttpResponse(200, {"bool_test": True})

You’ve encountered a self-inconsistent error message.

Yet if we look at the JSON standard, booleans are definitely JSON-compatible.

So, the error message is self-contradictory. What should we expect in that case? Nothing. Mathematically, it is not possible to draw valid conclusions from a contradictory statement.

We have to rely on observed behavior, instead, and if that’s not working, try encoding the value in some other way. Preferably, in a way that the receiving end will decode (correctly) and understand.

1 Like

Yeah I guess I could return an integer (0 or 1), but unless I’m doing something wrong here, I think this is an Anvil bug?

1 Like

This has been seen before. Search the Forum for postman bool, and you’ll see the prior discussions.

1 Like

Thank you! json.dumps() indeed doest the trick.

1 Like

Thanks for raising this - we’ll get that fixed

3 Likes