How to use HTTP Client API in Delphi - http

How to use NetHTTPClient instead of IdHTTP?
My NetHTTPClient code:
var
LHTTPClient: THTTPClient;
P : TStrings;
begin
LHTTPClient := THTTPClient.Create;
P := TStringList.Create;
try
Params.Add('test=' + Path);
Memo1.Lines.Text := NetHTTPClient1.Post('www.server.com', P).ContentAsString();
finally
LHTTPClient.Free;
end;
end;
My IdHTTP code:
procedure TForm1.Start;
var
lHTTP: TIdHTTP;
P: TStringList;
begin
lHTTP := TIdHTTP.Create(nil);
P := TStringList.Create;
try
Params.Add('test=' + Path);
try
Reply.Text := lHTTP.Post('www.server.com', P);
if AnsiContainsStr(Reply.Text, 'good') then
begin
Memo1.Lines.Add(Path);
end;
finally
end;
end;
end.

Neither one of your examples is technically correct. Your URLs are incomplete, and you are mismanaging your variable names.
Try this instead:
Indy:
procedure TForm1.Start;
var
LHTTP: TIdHTTP;
Params: TStringList;
begin
LHTTP := TIdHTTP.Create;
try
Params := TStringList.Create;
try
Params.Add('test=' + Path);
Memo1.Lines.Text := LHTTP.Post('http://www.server.com', Params);
finally
Params.Free;
end;
finally
LHTTP.Free;
end;
end.
NetHTTP:
procedure TForm1.Start;
var
LHTTP: TNetHTTPClient;
Params: TStringList;
begin
LHTTP := TNetHTTPClient.Create(nil);
try
Params := TStringList.Create;
try
Params.Add('test=' + Path);
Memo1.Lines.Text := LHTTP.Post('http://www.server.com', Params).ContentAsString;
finally
Params.Free;
end;
finally
LHTTP.Free;
end;
end;

Related

Request with query params fails

I have the following code to make an http request to a third party service:
... // more code
payl, err := json.Marshal(payload)
if err != nil {
result <- models.HHResult{
Result: "",
StatusCode: 0,
Error: err,
}
return
}
req, err := http.NewRequest(method, url, bytes.NewReader(payl))
q := req.URL.Query()
for k,v := range params {
q.Add(k,v)
}
req.URL.RawQuery = q.Encode()
client := http.Client{
Timeout: time.Millisecond * time.Duration(timeout),
}
resp, err := client.Do(req)
if err != nil {
result <- models.HHResult{
Result: "",
StatusCode: 0,
Error: err,
}
return
}
respbytes, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
result <- models.HHResult{
Result: string(respbytes),
StatusCode: resp.StatusCode,
Error: nil,
}
... // more code
The same works fine when not using query parameters. But when I do use params as :
tokenResponse = <-(apic.httpHelper.RequestWithOptions("GET", url,nil, map[string]string{
"user" : viper.GetString("userid"),
"password" : viper.GetString("pwd"),
The service is telling me I am missing required parameters.
If I print the request previously to actually doing it using curl/postman then it works fine.
What might I be doing wrong?
Third party service example:
https://auth.service.com/v1/oauth/token?user=matias&password=1234

How to send SMS using Delphi Code

I have made 3 attempts using Indy10.6.2 component, none of which show any errors, but the code is unable to send SMS. Please help me to send me the SMS through Delphi code.
Attempt 1
const
URL = 'https://api.bulksmsgateway.in/send/? username=****&hash=****&sender=TXTLCL&numbers=9198........&message=HISUNDAR';
//URL = 'https://api.textlocal.in/send/? username=*****&hash=******&sender=TXTLCL&numbers=9198...&message=HISUNDAR';
ResponseSize = 1024;
var
hSession, hURL: HInternet;
Request: String;
ResponseLength: Cardinal;
begin
hSession := InternetOpen('TEST', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
Request := Format(URL, [Username,Password,Sender,Numbers,HttpEncode(Message1)]);
hURL := InternetOpenURL(hSession, PChar(Request), nil, 0,0,0);
try
SetLength(Result, ResponseSize);
InternetReadFile(hURL, PChar(Result), ResponseSize, ResponseLength);
SetLength(Result, ResponseLength);
finally
InternetCloseHandle(hURL)
end;
showmessage(result);
finally
InternetCloseHandle(hSession)
end
Attempt 2
var
http : TIdHTTP;
IdSSL : TIdSSLIOHandlerSocketOpenSSL;
begin
http := TIdHTTP.Create(nil);
IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try
Http.ReadTimeout := 30000;
Http.IOHandler := IdSSL;
IdSSL.SSLOptions.Method := sslvTLSv1;
Http.Request.BasicAuthentication := True;
// IdSSL.SSLOptions.Method := sslvTLSv1;
//IdSSL.SSLOptions.Method := sslvTLSv1;
// http.Get('https://www.smsgatewayhub.com/api/mt/SendSMS? APIKey=B215dPone0yVIJU2QDH&senderid=TESTIN&channel=2&DCS=0&flashsms=0&number= 9195.....&text=test message&route=1');
http.Get('http://login.bulksmsgateway.in/sendmessage.php? user=****&password=****&mobile=95661....&message=Good Morning&sender=PRAPUS&type=3 ');
finally
http.Free;
end;
Attempt 3
var
lHTTP: TIdHTTP;
lParamList: TStringList;
lResult: String;
IdSSL : TIdSSLIOHandlerSocketOpenSSL;
begin
lParamList := TStringList.Create;
lParamList.Add('username=****');
lParamList.Add('password=****');
lParamList.Add('msgtext=Hello World');
lParamList.Add('originator=TestAccount');
lParamList.Add('phone=+9195....');
lParamList.Add('showDLR=0');
lParamList.Add('charset=0');
lParamList.Add('msgtype=');
lParamList.Add('provider=bulksmsgateway.in');
lHTTP := TIdHTTP.Create(nil);
try
lResult := lHTTP.Post('http://login.bulksmsgateway.in/sendmessage.php?', lParamList);
//WriteLn(lResult);
// Readln;
finally
FreeAndNil(lHTTP);
FreeAndNil(lParamList);
end;
You are sending the wrong parameters to the wrong URLs using the wrong HTTP methods. Per the code examples on the Bulk SMS Gateway website, you need to use HTTP POST with the correct URL and parameters. Please follow the online examples.
Try something more like this instead:
var
lHTTP: TIdHTTP;
lParamList: TStringList;
lResult: String;
IdSSL : TIdSSLIOHandlerSocketOpenSSL;
begin
lParamList := TStringList.Create;
try
lParamList.Add('user=****');
lParamList.Add('password=****' );
lParamList.Add('message=Hello World');
lParamList.Add('sender=TestAccount');
lParamList.Add('mobile=+9195....');
lParamList.Add('type=1'); // or 3
lHTTP := TIdHTTP.Create(nil);
try
// note: if you are using an up-to-date version of Indy,
// assigning the IOHandler is optional:
//
// http://www.indyproject.org/sockets/blogs/ChangeLog/20141222.aspx
//
lHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
try
lResult := lHTTP.Post('https://www.bulksmsgateway.in/sendmessage.php', lParamList);
// WriteLn(lResult);
// Readln;
except
on E: Exception do begin
//WriteLn('Error: ', e.Message);
end;
end;
finally
FreeAndNil(lHTTP);
end;
finally
FreeAndNil(lParamList);
end;
end;
If you want to send through the SMS Gateway Hub, you have a choice of using HTTP GET, or XML over HTTP POST:
var
lHTTP: TIdHTTP;
lParamList, lResult: String;
IdSSL : TIdSSLIOHandlerSocketOpenSSL;
begin
lParamList := Format('APIKey=%s&senderid=%s&channel=2&DCS=8&flashsms=0&number=%s&text=%s&route=1',
[
'****',
'TestAccount',
'9195....',
'Hello World'
]
);
lHTTP := TIdHTTP.Create(nil);
try
lHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
try
lResult := lHTTP.Get('https://www.smsgatewayhub.com/api/mt/SendSMS?' + lParamList);
// WriteLn(lResult);
// Readln;
except
on E: Exception do begin
//WriteLn('Error: ', e.Message);
end;
end;
finally
FreeAndNil(lHTTP);
end;
end;
var
lHTTP: TIdHTTP;
lParamList: TStringStream;
lResult: String;
IdSSL : TIdSSLIOHandlerSocketOpenSSL;
begin
lParamList := TStringStream.Create(
'<SmsQueue>' +
'<Account>' +
'<User>****</User>' +
'<Password>****</Password>' +
'<SenderId>TestAccount</SenderId>' +
'<Channel>1</Channel>' +
'<DCS>0</DCS>' +
'<FlashSms>0</FlashSms>' +
'<Route>1</Route>' +
'</Account>' +
'<Messages>' +
'<Message>' +
'<Number>9195....</Number>' +
'<Text>Hello World</Text>' +
'</Message>' +
'</Messages>' +
'</SmsQueue>',
TEncoding.UTF8);
try
lHTTP := TIdHTTP.Create(nil);
try
lHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
try
lHTTP.Request.ContentType := 'text/xml';
lHTTP.Request.Charset := 'utf-8';
lResult := lHTTP.Post('https://www.smsgatewayhub.com/RestAPI/MT.svc/mt', lParamList);
// WriteLn(lResult);
// Readln;
except
on E: Exception do begin
//WriteLn('Error: ', e.Message);
end;
end;
finally
FreeAndNil(lHTTP);
end;
finally
FreeAndNil(lParams);
end;
end;

IdHTTP how to send raw body

How i can use IdHTTP to send message as PostMan dos below:
My first attempt was as follow:
function TIdFoo.SendIM(const AID, AMessage: string): Boolean;
const
_URL = 'https://URL.com/SendMessage';
var
Params : TStringStream;
Response : string;
LMsg : string;
begin
Result := False;
LMsg := '-----------------------------13932'+
'Content-Type: application/json; charset=utf-8'+
'Content-Description: message'+ sLineBreak+ '{"message":{"Type":1,"body":"'+AMessage+'"},"to":["'+AID+'"]}'+
'-----------------------------13932--;'+sLineBreak;
Params := TStringStream.Create(LMsg, TEncoding.UTF8);
try
IdHTTP.Request.CustomHeaders.AddValue('authorization', 'Bearer ' + FToken);
IdHTTP.Request.CustomHeaders.AddValue('Origin', 'https://www.URL.com');
IdHTTP.Request.UserAgent := 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36';
IdHTTP.Request.Accept := '*/*';
IdHTTP.Request.Referer := 'https://www.URL.com/en-us/';
IdHTTP.Request.Host := 'URL.com';
IdHTTP.Request.AcceptEncoding := 'gzip, deflate, br';
IdHTTP.Request.AcceptLanguage := 'Accept-Language';
IdHTTP.Request.ContentType := 'multipart/mixed; boundary="---------------------------13932"';
Params.Position := 0;
try
Response := IdHTTP.Post(_URL, Params);
Result := True;
except
on E: Exception do
Writeln('Error on Send Message request: '#13#10, e.Message);
end;
Writeln(IdHTTP.Request.RawHeaders.Text);
finally
Params.Free;
end;
end;
The second attempt i try it this way
function TIdFoo.SendIM(const AID, AMessage: string): Boolean;
const
_URL = 'https://URL.com/SendMessage';
var
Params : TStringStream;
Response : string;
LMsg : string;
begin
Result := False;
LMsg := '{"message":{"Type":1,"body":"'+AMessage+'"},"to":["'+AID+'"]}';
Params := TStringStream.Create(LMsg, TEncoding.UTF8);
try
IdHTTP.Request.CustomHeaders.AddValue('authorization', 'Bearer ' + FToken);
IdHTTP.Request.CustomHeaders.AddValue('Origin', 'https://www.URL.com');
IdHTTP.Request.CustomHeaders.AddValue('Content-Description', 'message'); // I addedd this as on PostMan Body
IdHTTP.Request.UserAgent := 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36';
IdHTTP.Request.Accept := '*/*';
IdHTTP.Request.Referer := 'https://www.URL.com/en-us/';
IdHTTP.Request.Host := 'URL.com';
IdHTTP.Request.AcceptEncoding := 'gzip, deflate, br';
IdHTTP.Request.AcceptLanguage := 'Accept-Language';
IdHTTP.Request.ContentType := 'application/json; charset=utf-8'; // I alos changed this as it shown on PostMan body
Params.Position := 0;
try
Response := IdHTTP.Post(_URL, Params);
Result := True;
except
on E: Exception do
Writeln('Error on Send Message request: '#13#10, e.Message);
end;
Writeln(IdHTTP.Request.RawHeaders.Text);
finally
Params.Free;
end;
end;
Both attempts gives HTTP/1.1 400 Bad Request.
Can any advice my what i', doing wrong?
In your first example, your "raw" MIME data is not formatted correctly:
You are missing a bunch of required line breaks. And don't use the sLineBreak constant, as its value is platform-specific. MIME expects line breaks to use CRLF specifically. Indy has an EOL constant for that value.
You have an erroneous semicolon on the end of the closing boundary line.
You are also not setting the Request.AcceptEncoding property correctly. DO NOT enable encodings manually, unless you are prepared to actually handle them manually in responses (which your code is not). TIdHTTP handles gzip and deflate encodings for you, if you assign a TIdZLibCompressorBase-derived component, like TIdCompressorZLib, to the TIdHTTP.Compressor property. Don't worry about the br encoding, it is not widely used. In short, leave the Request.AcceptEncoding at its default and let TIdHTTP manage it for you.
You are also not setting the Request.AcceptLanguage property correctly. You should be setting it to 'en-US,en;q=0.8', not to 'Accept-Language'.
Your first example should work if you make these fixes, eg:
function TIdFoo.SendIM(const AID, AMessage: string): Boolean;
const
_URL = 'https://URL.com/SendMessage';
var
Params : TStringStream;
Response : string;
LMsg : string;
begin
Result := False;
LMsg := '-----------------------------13932' + EOL +
'Content-Type: application/json; charset=utf-8' + EOL +
'Content-Description: message' + EOL +
EOL +
'{"message":{"Type":1,"body":"'+AMessage+'"},"to":["'+AID+'"]}' + EOL +
'-----------------------------13932--' + EOL;
Params := TStringStream.Create(LMsg, TEncoding.UTF8);
try
IdHTTP.Request.CustomHeaders.AddValue('Authorization', 'Bearer ' + FToken);
IdHTTP.Request.CustomHeaders.AddValue('Origin', 'https://www.URL.com');
IdHTTP.Request.UserAgent := 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36';
IdHTTP.Request.Accept := '*/*';
IdHTTP.Request.Referer := 'https://www.URL.com/en-us/';
IdHTTP.Request.Host := 'URL.com';
IdHTTP.Request.AcceptLanguage := 'en-US,en;q=0.8';
IdHTTP.Request.ContentType := 'multipart/mixed; boundary="---------------------------13932"';
try
Response := IdHTTP.Post(_URL, Params);
Result := True;
except
on E: Exception do
Writeln('Error on Send Message request: '#13#10, e.Message);
end;
Writeln(IdHTTP.Request.RawHeaders.Text);
finally
Params.Free;
end;
end;
Alternatively:
function TIdFoo.SendIM(const AID, AMessage: string): Boolean;
const
_URL = 'https://URL.com/SendMessage';
var
Params : TMemoryStream;
Response : string;
LMsg : string;
begin
Result := False;
Params := TMemoryStream.Create;
try
WriteStringToStream(Params, '-----------------------------13932' + EOL);
WriteStringToStream(Params, 'Content-Type: application/json; charset=utf-8' + EOL);
WriteStringToStream(Params, 'Content-Description: message' + EOL);
WriteStringToStream(Params, EOL);
WriteStringToStream(Params, '{"message":{"Type":1,"body":"'+AMessage+'"},"to":["'+AID+'"]}' + EOL, IndyTextEncoding_UTF8);
WriteStringToStream(Params, '-----------------------------13932--' + EOL);
Params.Position := 0;
IdHTTP.Request.CustomHeaders.AddValue('Authorization', 'Bearer ' + FToken);
IdHTTP.Request.CustomHeaders.AddValue('Origin', 'https://www.URL.com');
IdHTTP.Request.UserAgent := 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36';
IdHTTP.Request.Accept := '*/*';
IdHTTP.Request.Referer := 'https://www.URL.com/en-us/';
IdHTTP.Request.Host := 'URL.com';
IdHTTP.Request.AcceptLanguage := 'en-US,en;q=0.8';
IdHTTP.Request.ContentType := 'multipart/mixed; boundary="---------------------------13932"';
try
Response := IdHTTP.Post(_URL, Params);
Result := True;
except
on E: Exception do
Writeln('Error on Send Message request: '#13#10, e.Message);
end;
Writeln(IdHTTP.Request.RawHeaders.Text);
finally
Params.Free;
end;
end;
Alternatively:
function TIdFoo.SendIM(const AID, AMessage: string): Boolean;
const
_URL = 'https://URL.com/SendMessage';
var
Params : TMemoryStream;
Response : string;
LMsg : string;
begin
Result := False;
Params := TMemoryStream.Create;
try
with TStreamWriter.Create(Params, TEncoding.UTF8) do
try
NewLine := EOL;
WriteLine('-----------------------------13932');
WriteLine('Content-Type: application/json; charset=utf-8');
WriteLine('Content-Description: message');
WriteLine;
WriteLine('{"message":{"Type":1,"body":"'+AMessage+'"},"to":["'+AID+'"]}');
WriteLine('-----------------------------13932--');
finally
Free;
end;
Params.Position := 0;
IdHTTP.Request.CustomHeaders.AddValue('Authorization', 'Bearer ' + FToken);
IdHTTP.Request.CustomHeaders.AddValue('Origin', 'https://www.URL.com');
IdHTTP.Request.UserAgent := 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36';
IdHTTP.Request.Accept := '*/*';
IdHTTP.Request.Referer := 'https://www.URL.com/en-us/';
IdHTTP.Request.Host := 'URL.com';
IdHTTP.Request.AcceptLanguage := 'en-US,en;q=0.8';
IdHTTP.Request.ContentType := 'multipart/mixed; boundary="---------------------------13932"';
try
Response := IdHTTP.Post(_URL, Params);
Result := True;
except
on E: Exception do
Writeln('Error on Send Message request: '#13#10, e.Message);
end;
Writeln(IdHTTP.Request.RawHeaders.Text);
finally
Params.Free;
end;
end;
In your second example, your "raw" data is just the JSON by itself, not any MIME wrapping it. You are putting MIME headers in the HTTP headers, where they don't belong. This example will not work if the server expects MIME data and not just raw JSON data.
You are also making the same mistakes with the Request.AcceptEncoding and Request.AcceptLanguage properties.
Since you are posting data in MIME format, an easier way to handle this would have been to use Indy's TIdMultipartFormDataStream class instead, and let it handle the MIME formatting for you. However, that class does not currently support:
setting the stream's RequestContentType property to a custom value (in this case, 'multipart/mixed' instead of 'multipart/form-data'). Though, you can use an accessor class to accomplish this, since the FRequestContentType member is protected.
omitting the Content-Disposition: form-data header on individual fields. This might trip up servers that are not expecting form-data submissions.
specifying the Content-Description MIME header at all (see Add support for user-defined MIME headers in TIdMultipartFormDataStream in Indy's issue tracker on GitHub).
So you will have to continue resorting to formatting the MIME data manually. You just have to make sure you get it right.

how to make http get request using TclientSocket?

I tried the following HTTP GET request
function CreateHTTPRequest(Site: String): String;
var
Request: String;
begin
Randomize;
Request := 'GET ' + Site + ' HTTP/1.1' + #13#10;
Request := Request + 'Host: ' + Site + #13#10;
Request := Request + 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' + #13#10;
Request := Request + 'Accept-Language: en-us,en' + #13#10;
Request := Request + 'User-Agent: ' + UserAgent + #13#10;
Request := Request + 'Referer: ' + Referer + #13#10;
Request := Request + 'Connection: close' + #13#10#13#10;
Result := Request;
end;
var
httpSocket: TClientSocket;
Site: String;
Target : string;
begin
httpSocket := TClientSocket.Create(nil);
httpSocket.Host := 'www.google.com';
httpSocket.Port := 80;
httpSocket.ClientType := ctBlocking;
httpSocket.Active := True;
if httpSocket.Socket.Connected=True then
begin
memo1.Lines.Add('requested');
Site := 'http://' + 'google.com';
httpSocket.Socket.SendText(CreateHTTPRequest(Site));
memo1.Lines.Add(httpSocket.Socket.ReceiveText);
httpSocket.Active := False;
end;
httpSocket.Free;
end;
I don't get any response from this. What did I do wrong? I cannot do any more HTTPS requests with TclientSocket. Is it dead already?

How can I upload a file to server, without browser, using go?

trsp := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
Url := "https://127.0.0.1:8080"
client := &http.Client{Transport: trsp}
request, _ := http.NewRequest("POST", Url, nil)
k, _ := os.Open(nameOfFile)
request.Header.Set("Action", "download"+k.Name())
...
...
client.Do(request)
I have server, and I need to upload to server a file. What should I do with request? As I think I shoud write into request.Body and then, from server handle this query
you need use the "mime/multipart"package to make the http body. like this.
http://matt.aimonetti.net/posts/2013/07/01/golang-multipart-file-upload-example/
func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
for key, val := range params {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
return nil, err
}
return http.NewRequest("POST", uri, body)
}

Resources