Connect Flask Restless API to React (Admin-on-Rest)

Asked

Viewed 126 times

1

I’m learning to work with flask and React to create a Rest API (with flask-Restless) and a Client (with React, Admin-on-Rest). I already created the Flask Api with Reastless.

I’m looking for some advice to connect the API to the client, but I can’t find what I need, if you know any froma or have some tricks, please tell me, it will be very useful.

  • You can’t find a library for that or you have one but you can’t get it to work?

  • I have the API working, I’m not and I can connect the API to admin-on-Rest, it can’t get the infection

1 answer

0

See if this helps buddy

<?php
    # Definindo pacotes de retorno em padrão JSON...
    header('Content-Type: application/json;charset=utf-8');

    # Carregando o framework Slim...
    require 'Slim/Slim.php';
    \Slim\Slim::registerAutoloader();

    # Iniciando o objeto de manipulação da API SlimFramework
    $app = new \Slim\Slim();
    $app->response()->header('Content-Type', 'application/json;charset=utf-8');

    # Função de teste de funcionamento da API...
    $app->get('/', function () {
        echo "Bem-vindo a API do Sistema de Clientes";
    });

    # Função para obter dados da tabela 'cliente'...
    $app->get('/clientes',function(){

        # Variável que irá ser o retorno (pacote JSON)...
        $retorno = array();

        # Abrir conexão com banco de dados...
        $conexao = new MySQLi("SERVIDOR","USUARIO_SERVIDOR","SENHA_DO_USUARIO","BANCO_DE_DADOS");

        # Validar se houve conexão...
        if(!$conexao){ echo "Não foi possível se conectar ao banco de dados"; exit;}

        # Selecionar todos os cadastros da tabela 'cliente'...
        $registros = $conexao->query("select * from cliente");

        # Transformando resultset em array, caso ache registros...
        if($registros->num_rows>0){
            while($cliente = $registros->fetch_array(MYSQL_BOTH)) {
                $registro = array(
                            "CODIGO"   => $cliente["CODIGO"],
                            "NOME"     => utf8_encode($cliente["NOME"]),
                            "TELEFONE" => $cliente["TELEFONE"],
                            "EMAIL"    => $cliente["EMAIL"],
                        );
                $retorno[] = $registro;
            }
        }

        # Encerrar conexão...
        $conexao->close();

        # Retornando o pacote (JSON)...
        $retorno = json_encode($retorno);
        echo $retorno;

    });

    # Executar a API (deixá-la acessível)...
    $app->run();
    ?>

In time, for the full functioning of these methods, we need to create and include one more file in the api folder. So create a new file in Notepad++, save it as . htaccess and enter the following source code:

RewriteEngine On

# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
  • I really appreciate the comment, but since I’m using python I don’t know how to use that code for what I need right now. But I’ll keep it because I can use it in the near future for my company

  • was here to see again the code that sent me and I was left with a doubt, this will not connect with React, serving as an intermediary between flask and React

  • truth friend will not give even, I will think of another way here

  • Thank you! When you get something say!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.