Have the following java code:
String apiKeyUserName = "your api username";
String apiKeyPassword = "your api password";
DefaultHttpClient httpclient = new DefaultHttpClient();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("fromPostcode", "2000"));
params.add(new BasicNameValuePair("toPostcode", "3000"));
params.add(new BasicNameValuePair("networkId", "01"));
params.add(new BasicNameValuePair("lodgementDate", "2013-08-01"));
String query = URLEncodedUtils.format(params, "UTF-8");
String urlPrefix = "api.auspost.com.au";
String validateURL = "https://" + urlPrefix + "/DeliveryDates.xml?";
HttpGet httpGet = new HttpGet(validateURL + query);
httpGet.addHeader("Cookie", "OBBasicAuth=fromDialog");
httpGet.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials(apiKeyUserName, apiKeyPassword),
"US-ASCII",false));
HttpResponse response = httpclient.execute(httpGet);
What I have tried:
Want to translate this into an Delphi. For the request I used TIdHttp as below:
procedure RequestDeliveryDate;
var
IdHTTP: TIdHTTP;
LHandler: TIdSSLIOHandlerSocketOpenSSL;
lResponse: String;
requestURL: String;
begin
IdHTTP := TIdHTTP.Create();
IdHTTP.Request.BasicAuthentication := True;
IdHTTP.Request.Username := 'your api username';
IdHTTP.Request.Password := 'your api password';
IdHTTP.Request.CharSet := 'utf-8';
LHandler := TIdSSLIOHandlerSocketOpenSSL.Create();
IdHTTP.IOHandler := LHandler;
requestURL := 'https://api.auspost.com.au/DeliveryDates.xml?fromPostcode=2000' +
'&toPostcode=3000' +
'&networkId=01' +
'&lodgementDate=2018-02-23' +
'&numberOfDates=01';
screen.Cursor := crHourGlass;
try
lResponse := IdHTTP.Get(requestURL);
screen.Cursor := crDefault;
except
on E: Exception do
begin
screen.Cursor := crDefault;
ShowMessage(E.Message);
end;
end;
IdHTTP.Free;
end;
Let's presume that I do supply the right api user name and password. When I call the above code I get the following error: "This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required."
What I'm doing wrong? Any suggestion?