Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. procedure TThreadBoleto.Execute;
  2. var
  3. RCtx : TRttiContext;
  4. RType : TRttiType;
  5. RProp : TRttiProperty;
  6. I : integer;
  7. PDF : TFile;
  8. begin
  9. try
  10. try
  11. RClient := TRESTClient.Create('');
  12. with RClient do begin
  13. AcceptEncoding := 'identity';
  14. FallbackCharsetEncoding := 'UTF-8';
  15. Accept := 'application/json;text/plain;application/pdf;';
  16. AcceptCharset := 'UTF-8';
  17. BaseURL := 'https://sandbox.boletocloud.com/api/v1/boletos';
  18. ContentType := 'application/x-www-form-urlencoded';
  19. HandleRedirects := true;
  20.  
  21. RCtx := TRttiContext.Create;
  22. RType := RCtx.GetType(THRBoleto.ClassType);
  23. I := 0;
  24. for RProp in RType.GetProperties do
  25. begin
  26. Params.AddItem;
  27. Params.Items[I].name := LowerCase(RProp.Name.Replace('_','.'));
  28. Params.Items[I].Value := RProp.GetValue(THRBoleto).AsString;
  29. I := I + 1;
  30. end;
  31. end;
  32.  
  33. RRequest := TRESTRequest.Create(RRequest);
  34. with RRequest do begin
  35. Accept := 'application/json;text/plain;application/pdf;';
  36. Client := RClient;
  37. Method := rmPost;
  38. SynchronizedEvents := false;
  39. AcceptCharset := 'UTF-8';
  40. end;
  41.  
  42. RResponse := TRESTResponse.Create(RResponse);
  43. RResponse.ContentType := 'application/pdf;*/*;';
  44. RResponse.ContentEncoding := 'UTF-8';
  45.  
  46. RAuth := THTTPBasicAuthenticator.Create('','');
  47. with RAuth do begin
  48. Username := 'anAPItokenAccess';
  49. Password := 'token';
  50. end;
  51.  
  52. RClient.Authenticator := RAuth;
  53. RRequest.Response := RResponse;
  54.  
  55. RRequest.Execute;
  56. PDF.WriteAllBytes(ExtractFilePath(Application.ExeName)+'boleto.pdf',RResponse.RawBytes);
  57. OutputStrings.Add(RResponse.Content);
  58. OutputStrings.Add('');
  59. OutputStrings.Add('');
  60. OutputStrings.AddStrings(RResponse.Headers);
  61. except on E:Exception do
  62. ShowMessage('Error: '+E.Message);
  63. end;
  64. finally
  65. THRBoleto.Free;
  66. end;
  67. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement