1

i want to post data to the following url:

http://mehratin.heroku.com/personals/new

i write the following code but has problem:

procedure TForm1.Button3Click(Sender: TObject);
var
  aStream: TMemoryStream;
  Params: TStringList;
begin
  aStream := TMemoryStream.Create;
  Params := TStringList.Create;
  try
    with IdHTTP1 do
    begin
      Params.Add('fname=123');
      Params.Add('lname=123');
      Request.ContentType := 'application/x-www-form-urlencoded';
      try
        Response.KeepAlive := False;
        Post('http://localhost:3000/personals/new', Params);
      except
        on E: Exception do
          showmessage('Error encountered during POST: ' + E.Message);
      end;
    end;

how can i post data by TIDHtttp.post method in delphi 2010?

3
  • You could try removing the Request.ContentType. Also - make sure the port you are trying to access is open, and not in use by another process. It is recommended to try this on your host instead. ;)
    – Jeff
    May 12, 2011 at 8:45
  • The TStrings version of Post() encodes the input data according to the 'application/x-www-form-urlencoded' content type by default (Indy 10.5.8)
    – mjn
    May 12, 2011 at 12:44
  • How does the error manifest, is there a HTTP error code, or bad data on the server side? Does the server expect application/x-www-form-urlencoded (or something else like multipart/form-data)?
    – mjn
    May 12, 2011 at 12:48

1 Answer 1

1

First things first, you'd need to read the http response code (it would have been useful to include that in your question).

In the absence of that I've used the Indy http object before as shown below. I included the parameters in my URL though. To troubleshoot, try running this with an http.Get to ensure the port is open, and you can actually connect to the server. Here's my example for completenes:

// parameters
params := format('export=1&format=%s&file=%s', [_exportType, destination]);

// First setup the http object
procedure TCrystalReportFrame.SetupHttpObject();
begin
    try
      IDHTTP1.HandleRedirects := TRUE;
      IDHTTP1.AllowCookies := true;
      IDHTTP1.Request.CacheControl := 'no-cache';
      IdHTTP1.ReadTimeout := 60000;
       _basePath:= GetBaseUrl;
    except
      on E: Exception do
        begin
          Global.LogError(E, 'SetupHttpObject');
        end;
    end;
end;

// Then have an onwork event
procedure TCrystalReportFrame.HttpWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
var
  Http: TIdHTTP;
  ContentLength: Int64;
  Percent: Integer;
begin
  Http := TIdHTTP(ASender);
  ContentLength := Http.Response.ContentLength;

end;

// The actual process
procedure TCrystalReportFrame.ProcessHttpRequest(const parameters: string);
var
  url : string;
begin
  try
      try
      SetupHttpObject;
       IdHTTP1.OnWork:= HttpWork;
       url := format('%s&%s', [_basePath, parameters]);
        url := IdHTTP1.Post(url);
      except
        on E: Exception do
          begin
            Global.LogError(E, 'ProcessHttpRequest');
          end;
      end;
    finally
      try
        IdHTTP1.Disconnect;
      except
        begin
        end;
      end;
    end;
end;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.