I am trying to write a client application which consumes a Mashape API service using Delphi XE3 and Indy10, but I have run into a bit of a snag.
Here is what I have tried:
I placed TIdHTTP
andTIdSSLIOHandlerSocketOpenSSL
components on my form and linked them together using the TIdHTTP.IOHandler
property. I then placed a button and memo on my form and placed the following bit of code on the buttons OnClick
event:
procedure TForm1.Button2Click(Sender: TObject);
begin
IdHTTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
IdHTTP1.Request.CustomHeaders.AddValue('X-Mashape-Key: ','<my_api_key>');
Memo1.Lines.Text := IdHTTP1.Get('https://hbrd-v1.p.mashape.com/anime/log-horizon');
end;
I then start up my app and when I press the button the app will wait for a moment and then spit out a HTTP/1.1 403 Forbidden
error message. Pressing the button a 2nd time will cause a HTTP/1.1 500 Internal Service Error
message.
I already checked to see if I had the needed SSL Library files on my system and I do, and I have tested my credentials time and time again and they appear to be correct, and I know the url is correct, so I must be missing something in my code to cause these errors to display. I was hoping someone with more experience in this area might be able to offer some advice.
Update: here is TRESTClient
code that works:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IPPeerClient, Vcl.StdCtrls,
REST.Response.Adapter, REST.Client, Data.Bind.Components,
Data.Bind.ObjectScope;
type
TForm1 = class(TForm)
RESTClient1: TRESTClient;
RESTRequest1: TRESTRequest;
Button1: TButton;
Memo1: TMemo;
RESTResponse1: TRESTResponse;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
RESTClient1.BaseURL := 'https://hbrd-v1.p.mashape.com/anime/log-horizon';
RESTRequest1.Execute;
Memo1.Lines.Text := RESTResponse1.Content;
// The only thing not shown here is the RESTRequest1.Params which are
// Name: 'X-Mashape-Key'
// Value: 'my-api-key'
// Kind: pkHTTPHEADER
// Everything else is included here which isn't much.
end;
end.
I want to do the same thing with TIdHTTP
.