Reputation: 5704
I am new to delphi and I need to send json string plus some additional files via single http post method, how should I go about this? I can with a solution that works, but since I am using TIdHTTP class the sent json test breaks, and becomes unusable, how should I go about this problem?
// Keiciam failu pavadinimus
if form1.ComboBox10.ItemIndex > 0 then
CopyFile(Pchar(form1.appdatadirStr + 'temp\Automatictmp.pdf'), Pchar(form1.appdatadirStr + 'temp\auto_' + OfferNumber + '.pdf'), true);
CopyFile(Pchar(form1.appdatadirStr + 'temp\vistmp.pdf'), Pchar(form1.appdatadirStr + 'temp\full_' + OfferNumber + '.pdf'), true);
CopyFile(Pchar(form1.appdatadirStr + 'temp\tmp.pdf'), Pchar(form1.appdatadirStr + 'temp\tech_' + OfferNumber + '.pdf'), true);
CopyFile(Pchar(form1.appdatadirStr + 'temp\' + uniquetmpfile + '.ini'), Pchar(form1.appdatadirStr + 'temp\vmss_' + OfferNumber + extension), true);
try
if form1.ComboBox10.ItemIndex > 0 then
filaspdf.AddFile('Automatic', form1.appdatadirStr + 'temp\auto_' + OfferNumber + '.pdf', 'multipart/form-data');
filaspdf.AddFile('AllFile', form1.appdatadirStr + 'temp\full_' + OfferNumber + '.pdf', 'multipart/form-data');
filaspdf.AddFile('UserFile', form1.appdatadirStr + 'temp\tech_' + OfferNumber + '.pdf', 'multipart/form-data');
filaspdf.AddFile('MainFile', form1.appdatadirStr + 'temp\vmss_' + OfferNumber + extension, 'multipart/form-data');
filaspdf.AddFormField('JSON',IvestiniaiJSON);
finally
IdHTTP.Post('http://prgcc.salda.lt/ventmaster_input', filaspdf); // Siunciam i WEB;
end;
filaspdf.Free;
Upvotes: 1
Views: 2163
Reputation: 596176
By default, AddFormField()
encodes string data using MIME's quoted-printable
transfer format. Some servers do not support that. You can disable it by setting the TIdFormDataField.ContentTransfer
property if needed:
filaspdf.AddFormField('JSON',IvestiniaiJSON).ContentTransfer := '';
Note that the default content type for AddFormField()
is text/plain
. The correct content type for JSON is application/json
instead:
with filaspdf.AddFormField('JSON',IvestiniaiJSON) do
begin
ContentType := 'application/json';
Charset := 'utf-8';
ContentTransfer := '';
end;
Some servers will not accept a content type specified on a text field at all. You can disable the content type if needed:
with filaspdf.AddFormField('JSON',IvestiniaiJSON) do
begin
ContentType := ' '; // note the space character
ContentTransfer := '';
end;
The whitespace is needed because the content type will be set to application/octet-stream
if you specify a blank string, but will be set to nothing if the property setter parses an empty string.
With that said, you are specifying the wrong content type when calling AddFile()
. multipart/form-data
is not a valid content type, it is a disposition instead, and TIdMultipartFormDtaStream
handles that internally for you. You need to specify the correct content type for the files themselves:
filaspdf.AddFile('Automatic', form1.appdatadirStr + 'temp\auto_' + OfferNumber + '.pdf', 'application/pdf');
Or let AddFile()
determine the content type for you based on the file extension:
filaspdf.AddFile('Automatic', form1.appdatadirStr + 'temp\auto_' + OfferNumber + '.pdf');
Upvotes: 5