Http request: invalid json in response

I am trying to create an application where an url is specified along with the number of closest matches (integer) to the content of that url. I have made a google cloud function which calculates this for a given link in the database. When running a similar request in python 3.7 using the requests package a json with the closest matches is returned but in anvil i keep getting the error: invalid json in response…
Can somebody help me?

form button

def searchsim_click(self, **event_args):
    urltext = str(self.url.text)
    ntext = int(self.n.text)
    table = {'url':urltext, 'n':ntext}
    resp = anvil.server.call('calculate_similarity', table)
    pass

server module

import anvil.server
import json

@anvil.server.callable
def calculate_similarity(table):
  table = json.dumps(table)
  print(json.dumps(table))
  resp = anvil.http.request("GCP link", 
                            json = table, method = "POST")
  return resp

Hi there,

Anvil’s HTTP module can take several named arguments, but the ones you’ll need for your calculate_similarity function are:

  • url
  • json (boolean - is this a JSON request?)
  • data (if json is set to True, this data will be JSON-encoded before sending)
  • method

Can you try editing your calculate_similarity function to look like this:

@anvil.server.callable
def calculate_similarity(table):
  resp = anvil.http.request(url="GCP link", 
                            json = True, data = table, method = "POST")
  return resp

You’ll also need to import the anvil.http module at the top of your ServerModule.

Let me know if this does the trick!