How to send HTTP data and get response with Synapse (Delphi) - http

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;

Related

how can i send a post request via Business Central?

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.

Uploading a bitmap with idHTTP from a Delphi 10.3 multidevice app

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

Add Cookie field to header of post request

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.

Calculate installation count with Inno Setup

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.

Unable to complete oauth web workflow for GitHub in golang

I'm trying to implement oauth-workflow for GitHub in golang and using https://github.com/franela/goreq to perform http(s) requests.
There is a section in which GitHub returns a code and you have to make a POST request to https://github.com/login/oauth/access_token with code, client_id and client_secret.
package main
import "fmt"
import "github.com/franela/goreq"
type param struct {
code string
client_id string
client_secret string
}
func main() {
params := param {code: "XX", client_id:"XX", client_secret: "XX"}
req := goreq.Request{
Method : "POST",
Uri : "https://github.com/login/oauth/access_token",
Body : params,
}
req.AddHeader("Content-Type", "application/json")
req.AddHeader("Accept", "application/json")
res, _ := req.Do()
fmt.Println(res.Body.ToString())
}
It is giving 404 with {"error":"Not Found"} message always.
While using Python, I'm getting the correct results with the same input data.
You are generating empty JSON objects. Your struct fields should start in capitals for the JSON encoder to be able to encode them.
type goodparam struct {
Code string `json:"code"`
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
}
See this in action.
You should double check your 'client_secret' and 'client_id' (must be right because you get the code) if it is correct, apparently Github returns HTTP status code 404 if it is wrong.

Resources