Skip to content

Instantly share code, notes, and snippets.

@viniciusfbb
Last active November 24, 2021 08:21
Show Gist options
  • Save viniciusfbb/0a5f8724c2f36a6a565546a530c64577 to your computer and use it in GitHub Desktop.
Save viniciusfbb/0a5f8724c2f36a6a565546a530c64577 to your computer and use it in GitHub Desktop.
Via CEP Api (Brazil Zip Code api) for Delphi
uses
iPub.Rtl.Refit; // From https://github.com/viniciusfbb/ipub-refit
type
TAddress = record
Erro: Boolean;
Cep: string;
Logradouro: string;
Complemento: string;
Bairro: string;
Localidade: string;
UF: string;
IBGE: string;
end;
[BaseUrl('https://viacep.com.br')]
IViaCEPApi = interface(IipRestApi)
['{3B497871-BC65-4A4D-A8C3-56AD6F607EB9}']
[Get('/ws/{ACEP}/json/')]
function TryGetAddress(const ACEP: string; out AResult: TAddress): Boolean;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
LAddress: TAddress;
begin
if GRestService.&For<IViaCEPApi>.TryGetAddress(edtCep.Text.Replace('-', '').Replace('.', ''), LAddress) then
begin
if LAddress.Erro then
Showmessage('Not found')
else
Memo1.Lines.Text := 'CEP: ' + LAddress.Cep + sLineBreak +
'End: ' + LAddress.Logradouro + sLineBreak +
'Compl: ' + LAddress.Complemento + sLineBreak +
'Bairro: ' + LAddress.Bairro + sLineBreak +
'Cidade: ' + LAddress.Localidade + sLineBreak +
'UF: ' + LAddress.UF + sLineBreak +
'Cod. IBGE: ' + LAddress.IBGE;
end
else
Showmessage('Service failed');
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment