Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Have the following java code:

Java
// Set your API key: remember to change this to your live API key in production
    String apiKeyUserName = "your api username";
    String apiKeyPassword = "your api password";

    // Set the query params
    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?
Posted
Updated 25-Feb-18 7:48am
Comments
Richard Deeming 23-Feb-18 16:46pm    
Your Delphi code doesn't seem to be setting the cookie: OBBasicAuth=fromDialog

Otherwise, you could try capturing the traffic from both versions to see what the difference is between the requests.
Member 11923969 25-Feb-18 5:12am    
Any suggestion on how to add it? Wasn't able to implemented ... 2-3 lines of code would be appreciated.
Member 11923969 25-Feb-18 12:55pm    
added the following code to the request:

IdHTTP.CookieManager := TIdCookieManager.Create(IdHTTP);
URI := TIdURI.Create(requestURL);
IdHTTP.CookieManager.AddServerCookie('OBBasicAuth=fromDialog', URI);
URI.Free;

... but I'm getting the same result back.
Logged the request - looks like this:
Sent 2/25/2018 7:47:18 PM: GET /DeliveryDates.xml?fromPostcode=2100&toPostcode=2001&networkId=01&lodgementDate=2018-02-25&numberOfDates=01 HTTP/1.1<eol>
Host: api.auspost.com.au<eol>
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8<eol>Accept-Encoding: identity<eol>
User-Agent: Mozilla/3.0 (compatible; Indy Library)<eol>
Authorization: Basic ODEzNmM2NTktOTY2OC00ZTM3LWFjMzItODkwMWJjNThlM2ZhOlN1Y2Nlc3MwMSE=<eol>
Cookie: OBBasicAuth=fromDialog<eol><eol>

 
Share this answer
 
Comments
Member 11923969 25-Feb-18 5:13am    
These 2 links do not show more than I already done in my code ...
It appears that is working with the CookieManager and also had to add '@auspost.com.au' to the provided API Key ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900