Code Monkey home page Code Monkey logo

jhonson's Introduction

jhonson

Jhonson is a official middleware for working with JSON in APIs developed with the Horse framework.
We created a channel on Telegram for questions and support:

⚙️ Installation

Installation is done using the boss install command:

boss install jhonson

If you choose to install manually, simply add the following folders to your project, in Project > Options > Resource Compiler > Directories and Conditionals > Include file search path

../jhonson/src

✔️ Compatibility

This middleware is compatible with projects developed in:

  • Delphi
  • Lazarus

⚡️ Quickstart Delphi

uses 
  Horse, 
  Horse.Jhonson, // It's necessary to use the unit
  System.JSON;

begin
  // It's necessary to add the middleware in the Horse:
  THorse.Use(Jhonson());
  
  // You can specify the charset when adding middleware to the Horse:
  // THorse.Use(Jhonson('UTF-8')); 

  THorse.Post('/ping',
    procedure(Req: THorseRequest; Res: THorseResponse; Next: TProc)
    var
      LBody: TJSONObject;
    begin
      // Req.Body gives access to the content of the request in string format.
      // Using jhonson middleware, we can get the content of the request in JSON format.
      
      LBody := Req.Body<TJSONObject>;
      Res.Send<TJSONObject>(LBody);
    end);

  THorse.Listen(9000);
end;

⚡️ Quickstart Lazarus

{$MODE DELPHI}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Horse, 
  Horse.Jhonson, // It's necessary to use the unit 
  fpjson, 
  SysUtils;

procedure PostPing(Req: THorseRequest; Res: THorseResponse; Next: TNextProc);
var
  LBody: TJSONObject;
begin
  // Req.Body gives access to the content of the request in string format.
  // Using jhonson middleware, we can get the content of the request in JSON format.
  LBody := Req.Body<TJSONObject>;
  Res.Send<TJSONObject>(LBody);
end;

begin
  // It's necessary to add the middleware in the Horse:
  THorse.Use(Jhonson);
  
  // You can specify the charset when adding middleware to the Horse:
  // THorse.Use(Jhonson('UTF-8')); 

  THorse.Post('/ping', PostPing);

  THorse.Listen(9000);
end.

⚠️ License

Jhonson is free and open-source middleware licensed under the MIT License.

jhonson's People

Contributors

andre-djsystem avatar bragaped avatar davisouzatb avatar dliocode avatar glerystonmatos avatar hunsche avatar snakeice avatar viniciussanchez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jhonson's Issues

Horse 3.01

Horse.OctetStream.pas(54): E2003 Undeclared identifier: 'THorseHackRequest'

Erro ao compilar no delphi Seatle

Ocorre um erro ao compilar no Delphi Seatle na seguinte linha:

procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED(FPC)}TNextProc{$ELSE}TProc{$ENDIF});
var
LJSON: {$IF DEFINED(FPC)}TJsonData{$ELSE}TJSONValue{$ENDIF};
begin
// if (Req.MethodType in [mtPost, mtPut, mtPatch]) and (Req.RawWebRequest.ContentType.Contains('application/json')) then
if (Req.MethodType in [mtPost, mtPut, mtPatch]) and (pos('application/json',Req.RawWebRequest.ContentType)>0) then

Resolvi desta forma, porém não sei se é a ideal.

Acentuação Linux

mesmo configurado o jhonson com "UFT-8" a acentuação esta sendo substituido por "?"

Ajuste no README.md

No DelphiXE7 ao tentar utilizar o código
THorse.Use( Jhonson );
Gera um erro de compilação informando que a variável está inválida
image

A solução é bem simples basta colocar o parênteses ao informar o uso do Jhonson.

THorse.Use( Jhonson() );

Com Isso sugiro rever os Middleware oficiais como o horse-compression por exemplo que também faz menção a forma de uso que apresenta erro.

Compiler warnings

Hello,

I get below compile time warnings with today's master branch:

[dcc32 Warning] Horse.Jhonson.pas(45): W1000 Symbol 'GetWebRequest' is deprecated: 'Dont use the THorseHackRequest class'
[dcc32 Warning] Horse.Jhonson.pas(51): W1000 Symbol 'SetBody' is deprecated: 'Dont use the THorseHackRequest class'
[dcc32 Warning] Horse.Jhonson.pas(56): W1000 Symbol 'GetWebResponse' is deprecated: 'Dont use the THorseHackResponse class'
[dcc32 Warning] Horse.Jhonson.pas(57): W1000 Symbol 'GetContent' is deprecated: 'Dont use the THorseHackResponse class'
[dcc32 Warning] Horse.JWT.pas(139): W1000 Symbol 'SetSession' is deprecated: 'Dont use the THorseHackRequest class'

I have no experience with Horse or jsonson, but it feels like some references needs to be changed.

Parser

Conforme postei no grupo do telegram da hasload, o Jhonson faz o parser mas não valida o json.
Para validar faltaria chamar o LJSON.toJSON e caso falhe já devolveria um status 400 automaticamente.
Enviei um vídeo exemplificando para o Vinicius.

Internal Application Error

Estou tentando rodar o exemplo do ping exatamente como esta aqui no repositório.
Estou usando o Delphi Tokyo 10.2 Version 25.0.29899.2631, e para testar a Url os clients RestDebugger e o Insomnia.
No client estou setando o método como POST, a URL: http://localhost:9000/ping, o content-type: application/json e o body da requisição com o json:
{"test":"test"}
Ao debugar parece que tudo esta executando corretamente, não consegui pegar nenhuma exceção. Aparentemente a linha

Res.Send<TJSONObject>(LBody);

executa sem problemas, mas tando no Insomnia quanto no RestDebugger estou recebendo um HTTP/1.1 500 Internal Server Error com o seguinte conteúdo:

<html><body><h1>Internal Application Error</h1>
<p>Invalid pointer operation
<p><hr width="100%"><i>/ping</i></body></html>

Se eu deixar o body da requisição vazio, o erro não ocorre.

Em anexo o projeto que estou testando.
HORSEAPI.zip

Erro ao tentar instalar o Middlewares jhonson. 🆘

Olá pessoal ao tentar instalar o Middlewares jhonson estou recebendo uma msg de erro que não sei oque esteja causando.

❯❯ backend 13:20 boss install jhonson
[WARN ] Please restart your console after complete.
[INFO ] Installing modules in project path
[INFO ] Building cache files...
[INFO ] Updating cache of dependency horse
[INFO ] Updating cache of dependency jhonson
[INFO ] Installing modules
[INFO ] Processing dependency horse
[WARN ] Tag not found , using semantic now...
[WARN ] No candidate to version for ^3.1.6. Using master branch
[ERROR] Error on switch to needed version from dependency github.com/hashload/horse

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.