Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Configuração Rest

Tdn:
https://tdn.totvs.com/pages/viewpage.action?pageId=553334690

Configuração do .ini:
;
;Configuracao REST
;
[HTTPSERVER]
Enable=1
Servers=HTTP_REST

[HTTP_REST]
hostname=localhost
port=9995
locations=HTTP_ROOT

[HTTP_ROOT]
Path=/
RootPath=D:\TOTVS\TOTVS 12\Microsiga\protheus_data\web\rest
DefaultPage=index.html
ThreadPool=THREAD_POOL

[THREAD_POOL]
Environment=TREINAMENTO
MinThreads=1
MaxThreads=4
GrowthFactor=1
InactiveTimeout=30000
AcceptTimeout=10000

Extraído os includes pelo comando:


appserver.exe -env=TLPPCORE -run=tlpp.environment.getIncludesTLPP

Verificando os serviços disponíveis:


http://localhost:9995/tlpp/rest/list/service
Exemplo de Fonte com Metodo Get para listagem de clientes:
#include 'tlpp-core.th'
#include 'tlpp-rest.th'

/*/{Protheus.doc} RESTTLPPDEMO
Treinamento desenvolvimento Rest em TLPP
@type Rest TLPP
@author Dataroute
@since 08/12/2022
/*/

@get("dataroute/clientes")
user function getListCliente()
local body := JsonObject():new() as Object
local oClient := JsonObject():new() as Object
local oListClient := JsonObject():new() as Object
local oParams := JsonObject():new() as Object
local aDados := {} as Array
local cQuery := " " as Character
local jHeader
local jQueryString
local cCodCli := "" as Character

RPCSetEnv("01","0101001")
//Define a resposta em formato Json
oRest:setKeyHeaderResponse('Content-Type','application/json')

//Captura as informacoes enviadas no body


body:fromJson(oRest:GetBodyRequest())
jHeader := oRest:getHeaderRequest()
jQueryString := oRest:getQueryRequest()

varinfo("body",body)
varinfo("jHeader",jHeader)
varinfo("jQueryString",jQueryString)

if jQueryString:GetJsonText("CodCliente") <> "null"


cCodCli := jQueryString:GetJsonText("CodCliente")
endif

cQuery := "select A1_COD, A1_NOME from "+retSqlName("SA1")+" "


cQuery += "where "
cQuery += "A1_FILIAL = '"+xFilial("SA1")+"' and D_E_L_E_T_ = ' ' "
if !empty(cCodCli)
cQuery += "and A1_COD = '"+cCodCli+"' "
else
cQuery += "and ROWNUM <= 100 "
endif

dbUseArea(.T.,"TOPCONN",TCGenQry(,,cQuery),"QTMPSA1",.T.,.T.)
DbSelectArea("QTMPSA1"); QTMPSA1->(DBGOTOP())
while QTMPSA1->(!eof())
oClient := JsonObject():new()
oClient["codigo"] := QTMPSA1->A1_COD
oClient["nome"] := alltrim(QTMPSA1->A1_NOME)

aAdd(aDados,oClient)
QTMPSA1->(dbSkip())
enddo

DbSelectArea("QTMPSA1");dbcloseArea()

//Retorno da Lista dos clientes


oListClient["clientes"] := aDados
oRest:setResponse(FwJsonSerialize(aDados))

//Definido Codigo de Erro de retorno


//oRest:setStatusCode(400) // Bad Request
//cReturn := '{"user":"","message":"invalid user"}'

//Apresentando erro no retorno


//Server marca o Status Code como 500
//oRest:setFault( '{"admin":"no"}' )
RPCClearEnv()

return

@Post("dataroute/clientes")
user function postCliente()
local body := JsonObject():new() as Object

//Define a resposta em formato Json


oRest:setKeyHeaderResponse('Content-Type','application/json')

//Captura as informacoes enviadas no body


body:fromJson(oRest:GetBodyRequest())

oRest:setResponse( '{"sample":"format"}' )

//Definido Codigo de Erro de retorno


//oRest:setStatusCode(400) // Bad Request
//cReturn := '{"user":"","message":"invalid user"}'

//Apresentando erro no retorno


//Server marca o Status Code como 500
//oRest:setFault( '{"admin":"no"}' )

return

Testando no Postman:
Informando CodCliente

Sem CodCliente:

You might also like