@Get/@Post

Decorators TLPP que expoem funcoes como endpoints REST automaticamente. Substitui WS_DEFINITION/WSMETHOD do AdvPL antigo.

Assinatura: @Get('/path') | @Post('/path') | @Put | @Delete

Retorna: construct

Decorators @Get, @Post, @Put, @Delete declaram que uma funcao TLPP e um endpoint REST. O AppServer roteia automaticamente pelo path — nao precisa configurar nada no appserver.ini alem do HTTPV11.

Sintaxe basica

#include 'tlpp-core.th'
#include 'tlpp-rest.th'

namespace archtec.api

@Get('/clientes')
function ListarClientes()
    local aClientes as array
    aClientes := CarregaClientes()

    httpResponse:setContentType('application/json')
    httpResponse:setBody(jsonObject():new():fromArray(aClientes):toJson())
return

GET com parametro de URL

@Get('/clientes/{id}')
function GetCliente()
    local cId as character
    local oJson as object

    cId := httpRequest:getURLParam('id')

    oJson := JsonObject():new()
    oJson['id']   := cId
    oJson['nome'] := 'Cliente ' + cId

    httpResponse:setContentType('application/json')
    httpResponse:setBody(oJson:toJson())
return

POST com body

@Post('/clientes')
function CriarCliente()
    local cBody as character
    local oReq  as object

    cBody := httpRequest:getBody()
    oReq := JsonObject():new()
    oReq:fromJson(cBody)

    // ... validar e gravar ...

    httpResponse:setStatus(201)
    httpResponse:setBody('{"created":true}')
return

Multiplos metodos

@Get('/health')
@Post('/health')
function Health()
    httpResponse:setBody('{"status":"ok"}')
return

Pegadinhas

Exemplos

Endpoint REST minimo

@Get('/ping')
function Ping()
    httpResponse:setBody('pong')
return

Veja também