I have a TIdHTTP component on a form, and I am sending an http POST request to a cloud-based server. Everything works brilliantly, except for 1 field: a text string with a plus sign, e.g. 'hello world+dog', is getting saved as 'hello world dog'.
Researching this problem, I realise that a '+' in a URL is regarded as a space, so one has to encode it. This is where I'm stumped; it looks like the rest of the POST request is encoded by the TIdHTTP component, except for the '+'.
Looking at the request through Fiddler, it's coming through as 'hello%20world+dog'. If I manually encode the '+' (hello world%2Bdog), the result is 'hello%20world%252Bdog'.
I really don't know what I'm doing here, so if someone could point me in the right direction it would be most appreciated.
I am using Delphi 2010. The component doesn't have any special settings, I presume I might need to set something? The header content-type that comes through in Fiddler is 'application/x-www-form-urlencoded'.
Then, the Delphi code:
Request:='hello world+dog'; URL :='http://............./ExecuteWithErrors'; TSL:=TStringList.Create; TSL.Add('query='+Request); Try begin IdHTTP1.ConnectTimeout:=5000; IdHTTP1.ReadTimeout :=5000; Reply:=IdHTTP1.Post(URL,TSL);Answer1:
You are using an outdated version of Indy and need to upgrade.
TIdHTTP's webform data encoder was changed several times in late 2010. Your version appears to predate all of those changes.
In your version,
TIdURI.ParamsEncode() internally to encode the form data, where a space character is encoded as
%20 and a
+ character is left un-encoded, thus:
In October 2010, the encoder was updated to encode a space character as
& before calling
In early December 2010, the encoder was updated to encode a space character as
+ instead, thus:
In late December 2010, the encoder was <em>completely re-written</em> to follow W3C's HTML specifications for
application/x-www-form-urlencoded. A space character is encoded as
+ and a
+ character is encoded as
In all cases, the above logic is applied only if the
hoForceEncodeParams flag is enabled in the
TIdHTTP.HTTPOptions property (which it is by default). If upgrading is not an option, you will have to disable the
hoForceEncodeParams flag and manually encode the
TStringList content yourself: