Http_endpoint [POST] Issues Publishing to Data Table

I think your response is an empty dictionary.

You should do something like this:

@anvil.server.http_endpoint('/new_alert',methods=['POST'])
def internet_stream(**response):
  bb = anvil.server.request.body.get_bytes()
  data = json.loads(bb)
  return app_tables.bb_internet.add_row(asn=data.get('asn'), ...

Also, try to add some prints so you can check the app log and see what’s in there:

@anvil.server.http_endpoint('/new_alert',methods=['POST'])
def internet_stream(**response):
  print(response)
  bb = anvil.server.request.body.get_bytes()
  data = json.loads(bb)
  print(data)
  return app_tables.bb_internet.add_row(asn=data.get('asn'), ...

The body_json should contain the json payload, but sometimes it doesn’t work, perhaps because my requests don’t have the correct header, so I use the body.

EDIT

A little cleanup:

@anvil.server.http_endpoint('/new_alert', methods=['POST'])
def internet_stream():
  bb = anvil.server.request.body.get_bytes()
  data = json.loads(bb)
  return app_tables.bb_internet.add_row(
      asn=data.get('asn'),
      hostnames=response.get('hostnames'),
      [...]
  )
2 Likes