I’m using the method to make requests to an API until the method stopped working. It’s showing the following error: HttpErrorStatus: Invalid JSON in response
at /downlink-sources/downlink-2023-11-22-11-25-32/anvil/http.py, line 82
Welcome to the forum!
Generally that error means the remote host returned something other than JSON. You might try calling it without the json parameter and printing out the result to see what it really is.
You can also try the remote call in a tool such as Insomnia, to get the Anvil bits out of the way for testing purposes.
So, the issue is that the API request was working for a long time, but it stopped working this week. I’ll provide the result from the API:
{
retorno: {
status_processamento: "3",
status: "OK",
pagina: 1,
numero_paginas: 2,
contas: [
{
conta: {
id: "739642114",
nome_cliente: "BAR E BOTEQUIM MG BORGES - EIRELI",
historico: "Ref. a NF nº 022481, BAR E BOTEQUIM MG BORGES - EIRELI",
numero_banco: "00834367270",
numero_doc: "022481/01",
serie_doc: "1",
data_vencimento: "26/07/2022",
situacao: "aberto",
data_emissao: "28/06/2022",
valor: "852.20",
saldo: "852.20"
}
},
Meu codigo:
@anvil.server.background_task
def incializaçao_get_contas_incremental(button_a_receber_incemental, todos_ids=[], paginas="&pagina="):
#------------- Base Comum da URl --------------------#
baseurl= "https://api.tiny.com.br/api2/"
token = "?token=" + anvil.secrets.get_secret('token')
formato = "&formato=json"
situacao = "&situacao="
paginas="&pagina="
id = "&id="
tipo_de_nota = "&tipoNota"
data_selecionada= "&dataAlteracao"
# Pesquisa de Contas Financeiras
situacao_str = ["aberto","pago"]
endpoint_r = "contas.receber.pesquisa.php"
relativepath_r = "conta.receber.obter.php"
endpoint_p = "contas.pagar.pesquisa.php"
relativepath_p = "conta.pagar.obter.php"
# Pesquisa de Produtos
situacao_str_produtos = ["A","I","E"]
endpoint_produtos = "produtos.pesquisa.php"
relativepath_produtos = "produto.obter.php"
relativepath_produtos_estoque ="produto.obter.estoque.php"
# Pesquisa de Pedidos
situacao_pedido = ['aberto','aprovado', 'preparando_envio','faturado']
endpoint_pedidos = "pedidos.pesquisa.php"
relativepath_pedidos = "pedido.obter.php"
# Pesquisa de Notas
situacao_notas = ['s','7']
endpoint_notas = "notas.fiscais.pesquisa.php"
relativepath_notas = "nota.fiscal.obter.php"
#------------ Pesquisa de Contas Financeiras -------------#
#----------------------------------------------------------#
#------------ Chamada Contas a Receber -------------#
chamada_a_receber = baseurl + endpoint_r + token + formato + situacao + situacao_str[0]
chamada_a_receber_ids = baseurl + relativepath_r + token + formato + id
#------------ Chamada Contas Recebidas -------------#
chamada_contas_recebidas = baseurl + endpoint_r + token + formato + situacao + situacao_str[1]
chamada_contas_recebidas_id = baseurl + relativepath_r + token + formato + id
#------------ Chamada Contas a Pagar -------------#
chamada_a_pagar = baseurl + endpoint_p + token + formato + situacao + situacao_str[0]
chamada_a_pagar_id = baseurl + relativepath_p + token + formato + id
#------------ Chamada Contas Pagas -------------#
chamada_pagas = baseurl + endpoint_p + token + formato + situacao + situacao_str[1]
chamada_pagas_id = baseurl + relativepath_p + token + formato + id
#-----------------------------------------------#
#------------ Pesquisa de Produtos -------------#
#-----------------------------------------------#
chamada_produtos = baseurl + endpoint_produtos + token + formato + situacao + situacao_str_produtos[0]
chamada_produtos_id = baseurl + relativepath_produtos + token + formato + id
chamada_produtos_estoque = baseurl + relativepath_produtos_estoque + token + formato + id
#-----------------------------------------------#
#------------ Pesquisa de Pedidos -------------#
#-----------------------------------------------#
chamada_pedidos = baseurl + endpoint_pedidos + token + formato + situacao + situacao_pedido[1]
chamada_pedidos_id = baseurl + relativepath_pedidos + token + formato + id
#-----------------------------------------------#
#------------ Pesquisa de Nota Fiscal ----------#
#-----------------------------------------------#
chamada_notas = baseurl + endpoint_notas + token + formato + tipo_de_nota + situacao_notas[0] + situacao + situacao_notas[1]
chamada_notas_id = baseurl + relativepath_notas + token + formato + id
texto = str(button_a_receber_incemental)
dicionario = {"Contas a Receber": [chamada_a_receber,chamada_a_receber_ids], "Contas Recebidas": [chamada_contas_recebidas,chamada_contas_recebidas_id], "Contas a Pagar":[chamada_a_pagar,chamada_a_pagar_id], "Contas Pagas": [chamada_pagas,chamada_pagas_id], "Produtos e Estoque": [chamada_produtos,chamada_produtos_id,chamada_produtos_estoque], "Notas Fiscais": [chamada_notas, chamada_notas_id], "Pedidos": [chamada_pedidos , chamada_pedidos_id]}
dicionario_bd = {"Contas a Receber": "contas_a_receber", "Contas Recebidas": "contas_recebidas_antigas", "Contas a Pagar": "contas_a_pagar", "Contas Pagas": "contas_pagas_antigas", "Produtos e Estoque": "produtos", "Notas Fiscais":"notas_fiscais", "Pedidos":"pedidos" }
nome_da_tabela = dicionario_bd[texto]
print(texto)
tabela = getattr(app_tables, nome_da_tabela)
valor = dicionario[texto]
valor_string = str(valor)
lista_digitos = list(valor_string)
if texto == 'Produtos e Estoque':
botao_1 = dicionario[texto][0]
botao_2 = dicionario[texto][1]
botao_3 = dicionario[texto][2]
else:
botao_1 = dicionario[texto][0]
botao_2 = dicionario[texto][1]
botao_3 = None
link = anvil.http.request(botao_1, json=True)
In this last line that I presented above, the error occurs, even though I had used this code several times to populate my database.
If you got that result from calling the API in an external tool such as Insomnia or https://resttesttest.com/ then the API is not returning valid JSON to you. In JSON the keys are strings using double quotes.
If you pass json=True
to anvil.http.request
and the API returns anything other than valid JSON format, you’ll get the error you originally posted.
I seem to recall that JSON5 can omit the quotes. Perhaps the API had a version change, and switched to JSON5?
Sorry, above I pasted the result from the JSON viewer that is installed in my browser
{"retorno":{"status_processamento":"3","status":"OK","produto":{"id":"726114110","nome":"SURF SAB\u00c3O EM PO ROSAS E FLOR DE LIS 800G","codigo":"0000000741774","unidade":"UN","saldo":0,"depositos":[{"deposito":{"nome":"Geral","desconsiderar":"N","saldo":0,"empresa":"bemlimp"}}]}}}
Regarding the … my API request lacks proper CORS headers in the response, so I’m unable to use the tool
Can you print(botao_1)
just before the call to anvil.http.request
so we know which API call is being made?
If RestTest doesn’t work for you, Insomnia will. It’s a standalone program, not running in a web browser, and so isn’t affected by CORS.