Hello Everyone,
PLease be patient. I’m almost a zero knowledge in coding. But I would like to learn. So please, be patient once the question will be veeeery basic:
I’m learning to use Knack and API with an simple app. The user fill the lawsuit number (processo) in a form and JS should request data from Court. Court provide API access to it (https://datajud-wiki.cnj.jus.br/api-publica/endpoints). I have tested the code using Google Colab and it did work out bringing all results I need.
import requests
import json
url = “https://api-publica.datajud.cnj.jus.br/api_publica_trt2/_search”
payload = json.dumps({
“query”: {
“match”: {
“numeroProcesso”: “######”
}
}
})
headers = {
‘Authorization’: ‘ApiKey cDZHYzlZa0JadVREZDJCendQbXY6SkJlTzNjLV9TRENyQk1RdnFKZGRQdw==’,
‘Content-Type’: ‘application/json’
}
response = requests.request(“POST”, url, headers=headers, data=payload)
print(response.text)
But I’m a little lost on how this workflow will work on Knack. Using GPT I asked to trasnlate this Python code into JavaScript
$(document).on(‘keyup’, ‘#view_74-field_1’, function() {
var numeroProcesso = $(‘#view_74-field_1’).val();
if (numeroProcesso) {
var apiUrl = 'https://api-publica.datajud.cnj.jus.br/api_publica_trt2/_search';
fetch(apiUrl, {
method: 'POST',
headers: {
'Authorization': 'ApiKey cDZHYzlZa0JadVREZDJCendQbXY6SkJlTzNjLV9TRENyQk1RdnFKZGRQdw==',
'Content-Type': 'application/json'
},
body: JSON.stringify({
query: {
match: {
numeroProcesso: numeroProcesso
}
}
})
})
.then(response => response.json())
.then(data => {
// Inserir os dados retornados em campos específicos do formulário
$('#view_74-field_16').val(data.hits.hits[0]._source.classe.nome); // Classe Processual
$('#view_74-field_17').val(data.hits.hits[0]._source.tribunal); // Tribunal
$('#view_74-field_102').val(data.hits.hits[0]._source.movimentos.map(mov => mov.nome).join(', ')); // Movimentos
// Atualizar os campos do formulário automaticamente
Knack.showSpinner();
Knack.views['view_74'].model.updateView();
})
.catch(error => {
console.error('Erro ao buscar dados do processo:', error);
});
}
});
But it’s not working. If I add the lawsuit number or update it, knack is not bringing the data. Where am I missing it?
thanks
Lucas