You can't post JSON data using the TStrings overload of TIdHTTP.Post(). That overload is designed for posting HTML webforms, and as such will post the data in "application/x-www-webform-urlencoded" format, which will corrupt your JSON (not that you are populating the TStringList correctly for JSON anyway, because you are not). That is why you are getting a "Bad request" error from the server about unexpected '%' characters.
Since you want to post the JSON in "application/json" format, you need to use the TStream overload of TIdHTTP.Post() instead:
Alternatively:
Since you want to post the JSON in "application/json" format, you need to use the TStream overload of TIdHTTP.Post() instead:
Code:
procedure TFDMFE.InviaFE(Autorizzazione, PercorsoFileXML, Credenziali, Dominio : string);
var
lHTTP: TIdHTTP;
lPostData: TStringStream;
JSON, Comando, RisultatoPost : string;
function EscapeForJson(const S: string): string;
var
I: Integer;
begin
Result := '';
for I := 1 to Length(S) do
begin
case S[I] of
#0..#7, #11, #14..#31:
Result := Result + '\u' + UpperCase(IntToHex(Ord(S[I]), 4));
#8:
Result := Result + '\b';
#9:
Result := Result + '\t';
#10:
Result := Result + '\n';
#12:
Result := Result + '\f';
#13:
Result := Result + '\r';
#34:
Result := Result + '\"';
#47:
Result := Result + '\/';
#92:
Result := Result + '\\';
else
Result := Result + S[I];
end;
end;
end;
begin
//************ COMANDO HTTP ********
Comando := 'https://ws.fatturazioneelettronica.aruba.it/services/invoice/upload';
//**********************************
JSON := '{' + sLineBreak +
' "dataFile" : "' + EncodeFile(PercorsoFileXML) + '",' + sLineBreak +
' "credential" : "' + EscapeForJson(Credenziali) + '",' + sLineBreak +
' "domain" : "' + EscapeForJson(Dominio) + '"' + sLineBreak +
'}';
lPostData := TStringStream.Create(JSON, TEncoding.UTF8);
try
lHTTP := TIdHTTP.Create;
try
lHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
lHTTP.HandleRedirects := True;
lHTTP.Request.CustomHeaders.FoldLines := False;
lHTTP.Request.Accept := 'application/json';
lHTTP.Request.CustomHeaders.Values['Authorization'] := 'Bearer ' + Autorizzazione;
lHTTP.Request.ContentType := 'application/json';
lHTTP.Request.CharSet := 'UTF-8';
try
RisultatoPost := lHTTP.Post(Comando, lPostData);
except
on E: EIdHTTPProtocolException do
begin
Memo1.Lines.Add(E.ErrorMessage);
end;
end;
finally
lHTTP.Free;
end;
finally
lPostData.Free;
end;
end;
Alternatively:
Code:
uses
..., System.JSON;
procedure TFDMFE.InviaFE(Autorizzazione, PercorsoFileXML, Credenziali, Dominio : string);
var
lHTTP: TIdHTTP;
lPostData: TStringStream;
lJSONObj: TJSONObject;
JSON, Comando, RisultatoPost : string;
begin
//************ COMANDO HTTP ********
Comando := 'https://ws.fatturazioneelettronica.aruba.it/services/invoice/upload';
//**********************************
lJSONObj := TJSONObject.Create;
try
lJSONObj.AddPair('dataFile', EncodeFile(PercorsoFileXML));
lJSONObj.AddPair('credential', Credenziali);
lJSONObj.AddPair('domain', Dominio);
JSON := lJSONObj.ToJSON;
finally
lJSONObj.Free;
end;
lPostData := TStringStream.Create(JSON, TEncoding.UTF8);
try
lHTTP := TIdHTTP.Create;
try
lHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
lHTTP.HandleRedirects := True;
lHTTP.Request.CustomHeaders.FoldLines := False;
lHTTP.Request.Accept := 'application/json';
lHTTP.Request.CustomHeaders.Values['Authorization'] := 'Bearer ' + Autorizzazione;
lHTTP.Request.ContentType := 'application/json';
lHTTP.Request.CharSet := 'UTF-8';
try
RisultatoPost := lHTTP.Post(Comando, lPostData);
except
on E: EIdHTTPProtocolException do
begin
Memo1.Lines.Add(E.ErrorMessage);
end;
end;
finally
lHTTP.Free;
end;
finally
lPostData.Free;
end;
end;