Upload image to website using multipart/form-data idhttp delphi - delphi

I need help in solving the problem of uploading a photo to wildberries.ru using IdHTTP. Post data looks like this when loading an image from the site:
------WebKitFormBoundaryCpJAFY6AlIHK7dZt
Content-Disposition: form-data; name="nmId"
13927988
------WebKitFormBoundaryCpJAFY6AlIHK7dZt
Content-Disposition: form-data; name="photos"; filename="13927988-2.jpg"
Content-Type: image/jpeg
{here these photos are most likely}
------WebKitFormBoundaryCpJAFY6AlIHK7dZt--
can't figure out how to transfer data via IdHTTP. I am using RAD Studio 10.3.1 Rio, Indy 10.

TIdHTTP has an overloaded Post() method that takes a TIdMultipartFormDataStream as input, which in turn has AddFormField() and AddFile() methods. For example:
uses
..., IdHTTP, IdSSLOpenSSL, IdMultipartFormData;
var
HTTP: TIdHTTP;
SSL: TIdSSLIOHandlerSocketOpenSSL;
begin
HTTP := TIdHTTP.Create;
try
SSL := TIdSSLIOHandlerSocketOpenSSL.Create(HTTP);
SSL.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
HTTP.IOHandler := SSL;
Data := TIdMultipartFormDataStream.Create;
try
with Data.AddFormField(‘nmId’, ‘13927988’) do
begin
ContentType := '';
ContentTransfer := '';
end;
with Data.AddFile(‘photos’, ‘full path to\13927988-2.jpg’, ‘image/jpeg’) do
ContentTransfer := '';
HTTP.Post(‘https://www.wildberries.ru/...’, Data);
finally
Data.Free;
end;
finally
HTTP.Free;
end;
end;

Related

Delphi EIdOSSLUnderlyingCryptoError Exception - SSL3_GET_RECORD Wrong Version Number

I tried using TRESTClient to connect to an HTTPS web service using TLS 1.2. But NO LUCK with sending multipart/form-data.
So now I am trying with Indy. I got this "Wrong Version Number" error.
I think there is nothing wrong with the code since it worked with HTTP.
Probably my Delphi is missing something. What should I install and how?
procedure TForm10.Button2Click(Sender: TObject);
var
HTTP: TIdHTTP;
RequestBody: TStream;
ResponseBody: string;
myssl: TIdSSLIOHandlerSocketOpenSSL;
Input: TIdMultipartFormDataStream;
begin
ResponseBody := '';
try
try
HTTP := TIdHTTP.Create;
try
Input := TIdMultipartFormDataStream.Create;
try
Input.Clear;
Input.AddFormField('Email', 'xx#xx.com.tr');
Input.AddFormField('Password', 'xx');
myssl := TIdSSLIOHandlerSocketOpenSSL.Create(HTTP);
HTTP.IOHandler := myssl;
myssl.SSLOptions.Mode := sslmUnassigned;
myssl.SSLOptions.Method := sslvTLSv1_2;
myssl.SSLOptions.SSLVersions := [sslvTLSv1_2];
HTTP.HTTPOptions := [hoForceEncodeParams];
HTTP.Request.CustomHeaders.FoldLines := False;
ResponseBody := HTTP.Post('https://xxx.com.tr/api/Mobile/MobileLoginControl', Input);
finally
Input.Free;
end;
finally
HTTP.Free;
end;
finally
end;
except
ResponseBody := '"-20"';
end;
end;
The code is fine, though you are enabling only TLS 1.2 on the SSLIOHandler. Maybe the website in question doesn't support TLS 1.2? Try enabling TLS 1.0 and 1.1 as well:
myssl.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
And don't set the SSLOptions.Method at all. Setting the SSLVersions updates the Method and vice versa. So set one or the other, not both.

TIdhttp HTTPS handshake Failed

I am using Indy 10 to connect to a SOAP Web Service. When I run the code with Fiddler running I find the webserver is not negotiating our Handshake. I have attached a screen shot of the Fiddler Inspectors.
We have been able to successfully connect to the server using the shareware application "SoapUI". I have attached a screen shot of the inspectors when using SoapUI.
One thing I notice is SoapUI is using a different version of SSL/TLS. How do I configure Indy to use the same version?
Any other insight as to why my call is failing?
procedure TForm6.Button1Click(Sender: TObject);
var
idhttp: TIdhttp;
IdSSLIOHandlerSocket1 : TIdSSLIOHandlerSocketOpenSSL;
Params : TStringStream;
begin
idhttp := TIdhttp.Create(nil);
IdSSLIOHandlerSocket1 := TIdSSLIOHandlerSocketOpenSSL.create(nil);
idhttp.ProxyParams.ProxyServer := '127.0.0.1';
idhttp.ProxyParams.ProxyPort := 8888 ;
idhttp.Request.ContentType := 'text/xml';
idhttp.Request.CharSet:='utf-8';
idhttp.request.connection := 'keep-alive';
idhttp.Request.CustomHeaders.AddValue('Content-Type','text/xml;charset=UTF-8');
idhttp.Request.CustomHeaders.AddValue('SOAPAction','"http://ws.nemsis.org/SubmitData"');
idhttp.Request.CustomHeaders.AddValue('Host','nemsis.org:443');
idhttp.Request.BasicAuthentication := True;
idhttp.request.username := 'xxxxx';
idhttp.request.password := 'yyyyy';
with IdSSLIOHandlerSocket1 do begin
SSLOptions.Method := sslvTLSv1_2;
SSLOptions.SSLVersions := [sslvTLSv1_2];
SSLOptions.VerifyMode := [];
SSLOptions.VerifyDepth := 2;
end;
with IdHTTP do begin
IOHandler := IdSSLIOHandlerSocket1;
end;
Params := TStringStream.Create(filetostring(ExtractFileDir(ParamStr(0))+'\sample.xml'));
idhttp.Post('https://nemsis.org:443/NemsisV3Validator4/NemsisWsService',Params);
end;
Failed Call:
Successful Call using SoapUI:

Can't open Microsoft Speech Recognition API via Post with Delphi

I want to send a Post request to Microsoft Speech Recognition API via HTTPS using Indy's TIdHTTP in Delphi.
On Microsofts Speech Recognition API Page: Microsoft Speech Recognition API Get started with speech recognition by using the REST API
they write you should send a HTTP POST Request like this:
POST https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language=en-US&format=detailed HTTP/1.1
Accept: application/json;text/xml
Content-Type: audio/wav; codec=audio/pcm; samplerate=16000
Ocp-Apim-Subscription-Key: YOUR_SUBSCRIPTION_KEY
Host: speech.platform.bing.com
Transfer-Encoding: chunked
Expect: 100-continue
I try this with Delphi XE 10 Indy.
But I ever got Error 400 - Bad Request as Answer!
What do I false in the following code?
procedure TForm1.Button1Click(Sender: TObject);
var
Response, csrf, url: String;
PostStream: TIdMultipartFormDataStream;
HTTPClient: TIdHTTP;
SSL: TIdSSLIOHandlerSocketOpenSSL;
begin
url := 'https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language=en-US&format=detailed HTTP/1.1';
HTTPClient := TIdHTTP.Create;
try
HTTPClient.Disconnect;
HTTPClient.AllowCookies := True;
SSL := TIdSSLIOHandlerSocketOpenSSL.Create(HTTPClient);
SSL.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
HTTPClient.IOHandler := SSL;
HTTPClient.HandleRedirects := true;
HTTPClient.Request.Accept := 'application/json;text/xml';
HTTPClient.Request.Method := 'POST';
HTTPClient.Request.ContentType := 'audio/wav; codec=audio/pcm; samplerate=16000';
//-----------------------------------------------------------------------
PostStream := TIdMultiPartFormDataStream.Create;
try
PostStream.AddFormField('Ocp-Apim-Subscription-Key','YOUR_SUBSCRIPTION_KEY');
PostStream.AddFile('file', 'test.wav');
Response := HTTPClient.Post(url, PostStream);
PostStream.Clear;
finally
PostStream.Free;
end;
finally
HTTPClient.Free;
end;
end;
Your POST request is not setup the way Microsoft's documentation says. Most importantly, you should not be using TIdMultipartFormDataStream at all, since the REST server is not expecting a request in multipart/form-data format. The body of the request is expected to be just the actual WAV file and nothing else. TIdHTTP even has an overload of Post() specifically for uploading just a file.
Try this instead:
procedure TForm1.Button1Click(Sender: TObject);
var
Response, url: String;
HTTPClient: TIdHTTP;
SSL: TIdSSLIOHandlerSocketOpenSSL;
begin
url := 'https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language=en-US&format=detailed';
HTTPClient := TIdHTTP.Create;
try
SSL := TIdSSLIOHandlerSocketOpenSSL.Create(HTTPClient);
SSL.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
HTTPClient.IOHandler := SSL;
HTTPClient.AllowCookies := True;
HTTPClient.HandleRedirects := true;
HTTPClient.Request.Accept := 'application/json;text/xml';
HTTPClient.Request.ContentType := 'audio/wav; codec=audio/pcm; samplerate=16000';
HTTPClient.Request.CustomHeaders.Values['Ocp-Apim-Subscription-Key'] := 'YOUR_SUBSCRIPTION_KEY';
Response := HTTPClient.Post(url, 'test.wav');
finally
HTTPClient.Free;
end;
end;

Using Indy to retrieve Revcontent OAuth Token

I am trying to use Indy to retrieve a RevContent oAuth token according to this page. Here is my code:
procedure TForm2.Button1Click(Sender: TObject);
var
IdSSLIOHandlerSocket1: TIdSSLIOHandlerSocketOpenSSL;
idhttp:tidhttp;
params: TStringList;
begin
idhttp:=tidhttp.create(self);
IdSSLIOHandlerSocket1 := TIdSSLIOHandlerSocketOpenSSL.create(nil);
with IdSSLIOHandlerSocket1 do begin
SSLOptions.Method := sslvSSLv23;
SSLOptions.SSLVersions := [sslvSSLv23];
end;
params := TStringList.create;
params.add('clientid=xxyyzz');
params.add('client_secret=xxyyzzxxyyzzxxyyzzxxyyzzxxyyzzxxyyzz');
params.add('grant_type=client_credentials');
with IdHTTP do begin
IOHandler := IdSSLIOHandlerSocket1;
Request.ContentType := 'application/x-www-form-urlencoded';
end;
showmessage(idhttp.Post('https://api.revcontent.io/oauth/token',params));
end;
But whenever I run it I get an 400 Bad Request error. What am I doing wrong?
Use client_id, not clientid.
Do not use SSL3, that site supports TLS1.
Do not set ContentType.
Use try/except around Post to catch exception and get full error message (E as EIdHTTPProtocolException).ErrorMessage

Post problems with Indy TIdHTTP

I am having issues posting to Amazon's SES Service using Indy's TIdHTTP.
Here is an example of the code i am using:
procedure TMainFrm.btnAmazonSESClick(Sender: TObject);
var
SSLHandler: TIdSSLIOHandlerSocket;
HttpClient: TIdHTTP;
Params: TStringStream;
begin
SSLHandler := TIdSSLIOHandlerSocket.Create(Self);
HttpClient := TIdHTTP.Create(Self);
Params := TStringStream.create('');
try
with SSLHandler do
SSLOptions.Method := sslvSSLv3
with HttpClient do
begin
IOHandler := SSLHandler;
AllowCookies := True;
HandleRedirects := True;
HTTPOptions := [hoForceEncodeParams];
Request.ContentType := 'application/x-www-form-urlencoded';
end;
PageMemo.Text := HttpClient.Post('https://email.us-east-1.amazonaws.com?Action=VerifyEmailAddress&AWSAccessKeyId=012Some123Key46&EmailAddress=test#test%2Ecom', Params);
finally
SSLHandler.Free;
HttpClient.Free;
Params.Free;
end;
end;
Result
Under Indy 10.5.7 I get the error: HTTP/1.1 404 Not Found
Under Indy 9.0.14 I get the error: Socket Error # 11004
Debugging Trials
This same demo can successfully GET the HTML from an HTTPS web page.
If i paste the URL above into a browser it displays the expected XML result.
I would appreciate any advice on the cause.
This post is just an incomplete wild guess.
Maybe Remy might help you to correct it. With the following code I'm getting HTTP/1.1 400 Bad Request but I'm not wondering because the API reference talks about Common Query Parameters where is at least required the digital signature you'll create for the request what I don't know how to do.
I can't test this at all because I have no account there. But I think the
procedure TForm1.Button1Click(Sender: TObject);
var
HTTPClient: TIdHTTP;
Parameters: TStrings;
SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
HTTPClient := TIdHTTP.Create(nil);
Parameters := TStringList.Create;
try
SSLHandler.SSLOptions.Method := sslvSSLv3;
SSLHandler.SSLOptions.Mode := sslmUnassigned;
HTTPClient.IOHandler := SSLHandler;
HTTPClient.HTTPOptions := [hoForceEncodeParams];
HTTPClient.Request.ContentType := 'application/x-www-form-urlencoded';
Parameters.Add('Action=VerifyEmailAddress');
Parameters.Add('EmailAddress=test#test.com');
Parameters.Add('AWSAccessKeyId=012Some123Key46');
Parameters.Add('SignatureVersion=2');
Parameters.Add('Expires='); // ???
Parameters.Add('Signature='); // ???
PageMemo.Text := HTTPClient.Post('https://email.us-east-1.amazonaws.com', Parameters);
finally
SSLHandler.Free;
HTTPClient.Free;
Parameters.Free;
end;
end;
Basically, you need to use the right library, i.e.:
For Indy 10.5.7 use openssl-1.0.1e-i386-win32 or openssl-1.0.1e-x64_86-win64 from http://indy.fulgan.com/SSL/
You may want to download an ssl demo from: http://indy.fulgan.com/ZIP/
Regards
Jose

Resources