Delphi's utf8encode, including \ r \ n at the end of the line

I need to send some data to a web service from my Delphi application. In short, what I do is

http := TIdHTTP.Create(nil); params := TIdMultiPartFormDataStream.Create; params.AddFormField('param', utf8encode('value')); http.Post('myurl', params); 

In the server log I get this

 {"param"=>"value\r\n"} 

I do not know if it is utf8encode or TidHTTP.post, which includes the CRLF at the end of the line. Any idea on how I can prevent this?

+4
source share
2 answers

TIdHTTP and TIdMultipartFormDataStream do not require extra line breaks. Also, the fact that you manually encode the text tells me that you're probably using an older version of Indy. In the current version of version 10.5.8, SVN AddFormField() has an ACharset parameter that sets the TIdFormDataField.CharSet property, so TIdMultipartFormDataStream can, if necessary, encode text inside itself, for example:

 http := TIdHTTP.Create(nil); params := TIdMultiPartFormDataStream.Create; params.AddFormField('param1', 'value1', 'utf-8'); params.AddFormField('param2', 'value2', 'utf-8'); params.AddFormField('param3', 'value3', 'utf-8'); http.Post('myurl', params); 
+3
source

Further research showed that TidHTTPClient and TIdMultiPartFormDataStream include '\ r \ n' up to the last parameter added. For instance,

 http := TIdHTTP.Create(nil); params := TIdMultiPartFormDataStream.Create; params.AddFormField('param1', utf8encode('value1')); params.AddFormField('param2', utf8encode('value2')); params.AddFormField('param3', utf8encode('value3')); http.Post('myurl', params); 

leads to

 {"param1"=>"value1", "param2"=>"value2", "param3"=>"value3\r\n"} 

Adding

 params.AddFormField('', ''); 

after all the parameters solve the problem. Not an ideal solution, but at the moment this is normal.

0
source

Source: https://habr.com/ru/post/1391818/


All Articles