0

I am trying to use the same procedure for two types of downloads. both are working.

I would like to use TDownloadURL or HTTP.Post on both, but cannot determine how. Which method and how to do this? Thank you.

First operation --

 procedure TfrmMain.get1Click(Sender: TObject);
 var
json: string;
lHTTP: TIdHTTP;
lParamList: TStringList;
result:string;
begin
  json := CRLF +
          '{' + CRLF +
          ' "resource_id": "391792b5-9c0a-48a1-918f-2ee63caa1c54",' + CRLF +
          ' "filters": {' + CRLF +
          '   "provider_id": 393303' + CRLF +
          ' }' + CRLF +
          '}';
  lParamList := TStringList.Create;
  try
    lParamList.Add('somename='+json);
    lHTTP := TIdHTTP.Create(nil);
    try
     Result := lHTTP.Post('http://hub.Healthdata.gov/api/action/datastore_search', lParamList);
    finally
      lHTTP.Free;
    end;
  finally
    lParamList.Free;
  end;
end;

Second operation --

procedure TfrmMain.get2Click(Sender: TObject); 
var
  dl: TDownloadURL;
  url:string;

begin
url:='http://api.census.gov/data/2010/sf1?key=KEY&get=P0010001,NAME&for=state:*';
 dl := TDownloadURL.Create(self);
  try
    dl.URL := url;
    dl.FileName := execpath+'api1.txt'; dl.ExecuteTarget(nil); dl.Free;
  except
    dl.Free;
  end;

end;
jachguate
  • 16,976
  • 3
  • 57
  • 98
kualoa
  • 17
  • 1
  • 4
  • I really don't understand what is your question about. – jachguate Apr 11 '13 at 16:06
  • I want to use only downloadurl or http.post for both urls (I have many of each type) ... I have not been able to get either one of them to work with both url structures. one url requires a parameter list and the other does not. How to get http.post to work with both? Thank you. – kualoa Apr 11 '13 at 17:03
  • 1
    Different URLs require different semantics. Why would you WANT to try to make the same code work for both? Your first operation is uploading data to the server. Your second operation is downloading a file from the server. They are not the same thing. `TDownloadURL` only sends HTTP `GET` requests. `TIdHTTP.Post()` only sends HTTP `POST` requests. `TIdHTTP` also has `Get()` methods for sending HTTP `GET` requests. – Remy Lebeau Apr 11 '13 at 18:13
  • You can't change that arbitrarily. A POST operation is different from a GET and that's the way the HTTP protocol works. If the sites are under your control, you can change the site requiring a GET to use a POST in order to use the same code. If the sites are not under your control, you can still ask the owner of one of the sites to provide a way to perform a POST to get the same result than the GET, or viceversa. – jachguate Apr 11 '13 at 19:21

1 Answers1

3

TDownloadURL uses the GET HTTP method. TIdHTTP.Post obviously uses the POST method. In general, neither is appropriate for use in place of the other. That's why both methods exist in the first place.

A POST request can include all the information that a GET request does, plus more, which makes it seem like it should be able to do everything GET can do, plus more. However, servers are not required to (and should not be expected to) handle POST requests the same way they do GET.

As the one writing the HTTP client, you're not really in control of the situation. The server dictates which methods it will honor. Clients need to either do what's expected of them or be denied access.

The Indy components support both methods, so if you just want to make your POST code and your GET code look similar, then you can replace TDownloadURL with TIdHTTP.Get.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467