FwHttpClient

Cliente HTTP REST moderno em TLPP. Substitui HttpPost/HttpGet legados. Suporta GET, POST, PUT, DELETE com headers customizados.

Assinatura: FwHttpClient():new(cUrl) -> oClient

Retorna: object

FwHttpClient e o cliente HTTP padrao de TLPP — substitui as funcoes antigas HttpPost/HttpGet. Suporta todos os metodos REST e headers customizados.

GET basico

local oClient as object
local nStatus as numeric
local cResp   as character

oClient := FwHttpClient():new('https://api.exemplo.com/clientes')
oClient:setSocketTimeout(30000)  // 30s

if oClient:get()
    nStatus := oClient:getHttpResponse():getStatusCode()
    cResp   := oClient:getHTTPResponse():getHTTPResponse()
    conout('Status: ' + cValToChar(nStatus))
endif

POST com JSON

local oClient as object
local oJson   as object

oJson := JsonObject():new()
oJson['cliente'] := '000001'
oJson['valor']   := 1500

oClient := FwHttpClient():new('https://api.exemplo.com/pedidos')
oClient:setHeader('Content-Type', 'application/json')
oClient:setHeader('Authorization', 'Bearer ' + cToken)
oClient:setBody(oJson:toJson())

if oClient:post()
    conout('Pedido criado')
else
    conout('Erro: ' + cValToChar(oClient:getHttpResponse():getStatusCode()))
endif

Pegadinhas

Parâmetros

NomeTipoObrigatórioDescrição
cUrlCharactersimURL completa do endpoint

Exemplos

POST simples

oClient := FwHttpClient():new(cUrl)
oClient:setBody(oJson:toJson())
oClient:post()

Veja também