Does anyone know what's the best approach to calculate installation count of an installer using Inno Setup? Can it be integrated with GA?
I read somewhere that by opening a PHP page at the end of installation, we can calculate installation count, but it is still too vague for me to understand.
For offline integration Google Analytics must be accessed via the new Measurement Protocol. Here is the same example from Martin but modified to access the Measurement Protocol:
[Code]
procedure CurStepChanged(CurStep: TSetupStep);
var
WinHttpReq: Variant;
Url: string;
begin
if CurStep = ssDone then
begin
try
Url := 'http://www.google-analytics.com/collect';
Log('Sending GA request: ' + Url);
WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
WinHttpReq.Open('POST', Url, False);
// see here the parameters : https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#required
WinHttpReq.Send('v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2mywebpage');
Log('GA request result: ' + IntToStr(WinHttpReq.Status) + ' ' + WinHttpReq.StatusText);
except
Log('Error sending GA request: ' + GetExceptionMessage);
end;
end;
end;
Read about the Measurement protocol.
You can use CurStepChanged event function to send HTTP request.
[Code]
procedure CurStepChanged(CurStep: TSetupStep);
var
WinHttpReq: Variant;
Url: string;
begin
if CurStep = ssDone then
begin
try
Url := 'http://....';
Log('Sending GA request: ' + Url);
WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
WinHttpReq.Open('GET', Url, False);
WinHttpReq.Send('');
Log('GA request result: ' + IntToStr(WinHttpReq.Status) + ' ' + WinHttpReq.StatusText);
except
Log('Error sending GA request: ' + GetExceptionMessage);
end;
end;
end;
Not sure however what URL you need to use to link it to GA. There's another question that covers this part of the problem:
Using Google Analytics without Javascript?
You can of course query your own web page (e.g. PHP) that does the calculation.
Related
Can't upload image to my Wordpress site via REST api in Delphi 10.4.
I published a post via the REST api (in the REST Debugger and directly in the program), but without pictures it makes no sense.
Most recent code with RESTClient components:
procedure TForm1.Button1Click(Sender: TObject);
var jpgFoto: TMemoryStream;
begin
HTTPBasicAuthenticator1.Username := 'myuser';
HTTPBasicAuthenticator1.Password := 'mypass';
RESTRequest1.Method := TRESTRequestMethod.rmPOST;
RESTRequest1.Resource := 'wp/v2/media';
RESTClient1.BaseURL := 'https://*.ru/wp-json';
RESTRequest1.Params.AddHeader('Content-Disposition', 'attachment; filename="00s.jpg"');//I tried different options
RESTRequest1.Params.AddHeader('Content-Type', 'image/jpeg');//tried different types, for example multipart or without this line
RESTRequest1.Params.AddItem('data-binary', 'D:\Europe\00s.jpg');//does it make sense?
RESTRequest1.Params[0].Kind := pkGETorPOST;
jpgFoto := TMemoryStream.Create;
jpgFoto.LoadFromFile('D:\Europe\00s.jpg');
RESTRequest1.AddBody(jpgFoto, TRESTContentType.ctIMAGE_JPEG);
jpgFoto.Position := 0;
try
RESTRequest1.Execute;
except
memo1.Text := RESTResponse1.Content;
end;
jpgFoto.Free;
end;
raised exceptions: rest_upload_invalid_disposition, rest_upload_invalid_disposition or rest_upload_no_data (depends on the settings I used in Header RESTRequest)
I found a solution that helped at least one person: switch to INDY to the idHTTP component. But there are no details. I couldn't login through the Basic Authentication method. I set parameters in IDHTTP.Request.Username and IDHTTP.Request.Password, but received a response from Wordpress - 403 Forbidden. It's not clear format to fill params, in JSON or just parameter = value.
I just started learning REST and am stuck for so many hours. Please do not leave in trouble, help upload images in Wordpress api REST with delphi REST or Indy components or any other working way, I will be sooo grateful
It's work:
var
Params: TIdMultipartFormDataStream;
begin
Params := TIdMultipartFormDataStream.Create;
Params.AddFormField('content-type','image/jpeg');
Params.AddFile('file', 'c:\images\sdsd.jpg', '');
idhttp2.Request.Accept := 'application/json; charset= UTF-8';
idhttp2.Request.Authentication := TIdBasicAuthentication.Create;
idhttp2.Request.Authentication.Username:= 'Username';
idhttp2.Request.Authentication.Password := 'Password';
idhttp2.Request.BasicAuthentication := true;
idhttp2.Request.ContentDisposition := 'form-data; filename="anyname.jpg"';//
idhttp2.Request.ContentType := 'application/json; charset= UTF-8';
try
idhttp2.Post('htts://mysite/wp-json/wp/v2/media/', Params);
except
memo1.Text := idhttp2.ResponseText;
end;
end;
I would like to create a PostRequest in my Business Central Extension that authenticates me in my web service and returns me a token. I send my username and password in the body of the request to my web service and I also receive the token in JSON format in the body.I want to create the post request using HttpClient.
I use the following link as a template: https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/httpcontent/httpcontent-data-type
procedure sendPostRequest(uri: Text)
var
client: HttpClient;
content: HttpContent;
contentHeaders: HttpHeaders;
response: HttpResponseMessage;
request: HttpRequestMessage;
begin
content.GetHeaders(contentHeaders);
contentHeaders.Clear();
contentHeaders.Add('Content-Type', 'application/json');
request.Content:= content;
request.SetRequestUri(uri);
request.Method := 'POST';
end;
procedure SetURLsToDefault(var MessagingServiceSetup: Record "Messaging Service Setup WMR")
begin
MessagingServiceSetup."Service URL" := '202.212.127:8800';
end;
And I have a couple of questions:
1) the basic url is 202.212.127:8800 for my API gateway. To be able to authenticate myself I have to access 202.212.127:8800/authenticate. Is there a method in which you can create urls?
2) how do I get my username and password in the content?
3) and how do I get the token and can I save it in the field?
can someone tell me how to get the PostRequest up and running?
Common method to create different URLs is like this:
Create a setup table
Create fields like "Base Url", User, Pass etc.
I propose this pattern for your code:
SendRequest(Method; Url; Body)
Begin
...
Couple of functions (Your Api and Auth):
Authenticate()
begin
Method = 'post';
Url = SetupTable."Base Url" + '/authenticate';
Body = (Use AL Json stack and incorporate your user pass)
SendRequest(Method; Url; Body);
end;
Function1()
begin
Method = 'get';
Url = SetupTable."Base Url" + '/apiPath-Function1';
Body = '';
SendRequest(Method; Url; Body);
end
Function2()
begin
Method = 'post';
Url = SetupTable."Base Url" + '/apiPath-Function2';
Body = (Use AL Json stack and incorporate your body structure);
SendRequest(Method; Url; Body);
end;
To get your user pass into the content you need to check the documentation of the Api you're trying to call. It's usually described in details, it can be a simple header for basic authentication or a complex Jwt.
For receiving a token, again you need to check your Api documentation first, but essentially after making a Rest call (like: client.Send(RequestMessage, ResponseMessage); inside your SendRequest method), you get a response back and you can use AL Json stack to carve information out.
This is a fine article on how to proceed:
https://jackmallender.com/2019/03/04/interacting-with-rest-apis-using-json-from-within-business-central-part-1-an-introduction-to-the-httpclient-data-type/
Basically a string could work as an url. Depends on what you want. It is good practice to have a setup for your web service calls, so I am with Babak. You can set up a table in which you store the links, credentials - whatsoever.
and 4) I suggest Waldos Rest App for web service calls. you can download the source here: https://github.com/waldo1001/waldo.restapp
It encapsulated the calls, has helper functions for json handling as well. Using the "REST Helper" Codeunit. You can break down your call to:
local procedure DoCallWebservice(URI: Text; User: Text; Pass: Text; var Token: Text);
var
RESTHelper: Codeunit "REST Helper WLD";
begin
RRESTHelper.Initialize('GET', URI);
RESTHelper.SetContentType('application/json');
RESTHelper.AddBody('{"user":"USERNAME","pass":"PASSWORD"}');
if RESTHelper.Send() then
Token := RESTHelper.GetResponseContentAsText();
end;
Obviously, you need to parse the response (JSONHelper) to your needs. Look at the code of the codeunit, it's more or less self explanatory.
I have read many related posts about sending data with idHTTP but still I can't manage it.
I use this code :
updated
procedure TTabbedForm.SpeedButton1Click(Sender: TObject);
var
fName : string;
mStream : TMemoryStream;
begin
fName := 'image.jpg';
mStream := TMemoryStream.Create;
myImage.Bitmap.SaveToStream(mStream);
mStream.Position := 0;
try
IdHTTP1.Request.ContentType := 'application/octet-stream';
IdHTTP1.PUT('http://www.example.com/'+fName, mStream);
finally
mStream.free;
end;
end;
but i receive the error "Method not allowed".
What i'm doing wrong, please ?
For uploads to Google Drive, some additional steps are required. For example, the HTTP POST request must include a auth token which in turn is provided to you only after authentication (log in with a Google account). For Google Drive you must also use secure connections (https) which require SSL libraries such as OpenSSL.
Example from the API docs:
POST https://www.googleapis.com/upload/drive/v3/files?uploadType=media HTTP/1.1
Content-Type: image/jpeg
Content-Length: [NUMBER_OF_BYTES_IN_FILE]
Authorization: Bearer [YOUR_AUTH_TOKEN]
[JPEG_DATA]
The file simple upload API for Google Drive is documented here:
https://developers.google.com/drive/api/v3/simple-upload
Update
Try this example, it requires a valid auth token:
procedure TDriveAPITest.Run;
var
PostData: TStream;
Response: string;
begin
PostData := TFileStream.Create('test.png', fmOpenRead or fmShareDenyWrite);
try
IdHTTP := TIdHTTP.Create;
try
IdHTTP.HTTPOptions := IdHTTP.HTTPOptions + [hoNoProtocolErrorException];
IdHTTP.Request.CustomHeaders.Values['Authorization'] := 'Bearer [YOUR_AUTH_TOKEN]';
Response := IdHTTP.Post('https://www.googleapis.com/upload/drive/v3/files?uploadType=media', PostData);
if IdHTTP.ResponseCode = 200 then begin
WriteLn('Response: ' + Response);
end else begin
WriteLn('Error: ' + IdHTTP.ResponseText);
end;
finally
IdHTTP.Free;
end;
finally
PostData.Free;
end;
end;
Output:
Error: HTTP/1.0 401 Unauthorized
I would like to send post request like following by program is written in delphi.
There is a cookie field in request header (in red rectangle).
I wrote following source code.
procedure TForm1.Button2Click(Sender: TObject);
var
uri : TIdURI;
cookie : TIdCookieManager;
HTTP : TIdHTTP;
vals: TStringList;
url : String;
response : TStringStream;
begin
HTTP := TIdHTTP.Create();
HTTP.AllowCookies := True;
HTTP.Request.ContentType := 'application/x-www-form-urlencoded';
HTTP.HandleRedirects := True;
cookie := TIdCookieManager.Create();
uri := TIdURI.Create('www.hoge.com');
cookie.AddServerCookie('ASP.NET_SessionId=test', uri);
HTTP.CookieManager := cookie;
vals := TStringList.Create;
response := TStringStream.Create('');
vals.Add('__EVENTTARGET=');
vals.Add('__EVENTARGUMENT=');
vals.Add('__VIEWSTATE=/wEPDwUINzcxNjQyMjkPFgIeE1ZhbGlkYXRlUmVxdWVzdE1vZGUCAWRkEHyFbwQQE8tM5FKRr3UELd00osRNQBzu31XZl1yd86A=');
vals.Add('__VIEWSTATEGENERATOR=A7C0DD1C');
vals.Add('__EVENTVALIDATION=/wEdAAZkcRcs1jgA2rEUAtpu7qzIhFuNiVVNuLciluwM7Vty0gJCK50467l5FRCktGxHOlNKe/Y7d9SBufbGEp2w5OLHqFe59uEio+bAp/33YZOR3aKeKEbp39eHc9mbdvkCgxAPflO5NLAHc5uwdZn6JOnwKMi9h+dluqFLpmg3gO25cg==');
vals.Add('ddlLanguage=ja-JP');
vals.Add('tbUserId=myid');
vals.Add('tbPassword=hoge');
vals.Add('btnLogin=login');
url := TIdURI.ParamsEncode('ReturnUrl=/GssNet/main/default.aspx');
url := TIdURI.URLEncode('www.hoge.com/GssNet/login,aspx?ReturnUrl=/GssNet/main/default.aspx');
try
HTTP.Post('http://www.hoge.com/GssNet/login,aspx', vals, response);
Except
on EIdHTTPProtocolException do
begin
ShowMessage(Memo1.TextHint);
end;
end;
end;
But cookie field is not included in request header. Following is result of packet capture. when I execute my program. Please teach me the way to add cookie field to request header.
As #smooty86 said in comments, you need to include the http:// portion of the URL when adding a cookie manually. You also need to include the full path to the resource that is being requested, otherwise the cookie will only be valid for requests to the root / path.
Also, your calls to TIdURI.ParamsEncode() and TIdURI.URLEncode() are useless since you are not using the encoded url variable. If you are going to take the time to encode a URL than make sure to actually use it.
You are also leaking all of your objects.
Try this instead:
procedure TForm1.Button2Click(Sender: TObject);
var
uri : TIdURI;
HTTP : TIdHTTP;
vals : TStringList;
url : String;
response : TStringStream;
begin
try
response := TStringStream.Create('');
try
vals := TStringList.Create;
try
vals.Add('__EVENTTARGET=');
vals.Add('__EVENTARGUMENT=');
vals.Add('__VIEWSTATE=/wEPDwUINzcxNjQyMjkPFgIeE1ZhbGlkYXRlUmVxdWVzdE1vZGUCAWRkEHyFbwQQE8tM5FKRr3UELd00osRNQBzu31XZl1yd86A=');
vals.Add('__VIEWSTATEGENERATOR=A7C0DD1C');
vals.Add('__EVENTVALIDATION=/wEdAAZkcRcs1jgA2rEUAtpu7qzIhFuNiVVNuLciluwM7Vty0gJCK50467l5FRCktGxHOlNKe/Y7d9SBufbGEp2w5OLHqFe59uEio+bAp/33YZOR3aKeKEbp39eHc9mbdvkCgxAPflO5NLAHc5uwdZn6JOnwKMi9h+dluqFLpmg3gO25cg==');
vals.Add('ddlLanguage=ja-JP');
vals.Add('tbUserId=myid');
vals.Add('tbPassword=hoge');
vals.Add('btnLogin=login');
HTTP := TIdHTTP.Create;
try
HTTP.HandleRedirects := True;
HTTP.AllowCookies := True;
HTTP.CookieManager := TIdCookieManager.Create(HTTP);
uri := TIdURI.Create('http://www.hoge.com/GssNet/login,aspx');
try
HTTP.CookieManager.AddServerCookie('ASP.NET_SessionId=test', uri);
finally
uri.Free;
end;
url := TIdURI.URLEncode('http://www.hoge.com/GssNet/login,aspx?ReturnUrl=/GssNet/main/default.aspx');
HTTP.Request.ContentType := 'application/x-www-form-urlencoded';
HTTP.Post(url, vals, response);
finally
HTTP.Free;
end;
finally
vals.Free;
end;
// use response as needed...
finally
response.Free;
end;
except
on EIdHTTPProtocolException do
begin
ShowMessage(Memo1.TextHint);
end;
end;
end;
Lastly, why are you sending a request to login,aspx? The correct name is login.aspx instead. login,aspx does not exist on the server.
I think the title was clear enough.
I want to know how to send HTTP POST request with parameters/arguments and receive HTML response back - using Synapse library for Delphi.
Try to use HttpPostURL function.
function HttpPostURL(const URL, URLData: string; const Data: TStream): Boolean;
URL - target URL
URLData - URL parameters; must be encoded e.g. using EncodeURLElement function
Data - target stream, where the response will be stored
The following example uses testing POST server where send two POST parameters. Note using of EncodeURLElement function for encoding parameter data. If the POST succeed the server response is saved into the file.
uses HTTPSend, Synacode;
procedure TForm1.Button1Click(Sender: TObject);
var URL: string;
Params: string;
Response: TMemoryStream;
begin
Response := TMemoryStream.Create;
try
URL := 'http://posttestserver.com/post.php?dump&html';
Params := 'parameter1=' + EncodeURLElement('data1') + '&' +
'parameter2=' + EncodeURLElement('data2');
if HttpPostURL(URL, Params, Response) then
Response.SaveToFile('c:\response.txt');
finally
Response.Free;
end;
end;