problem with post methos https - URGENT - Printable Version +- Atozed Forums (https://www.atozed.com/forums) +-- Forum: Indy (https://www.atozed.com/forums/forum-8.html) +--- Forum: Indy General Discussion (https://www.atozed.com/forums/forum-9.html) +--- Thread: problem with post methos https - URGENT (/thread-978.html) |
problem with post methos https - URGENT - staff@ergosoft.it - 03-01-2019 Hi, I have this function function TFDMFE.EncodeFile(const FileName: string): AnsiString; var stream: TMemoryStream; begin stream := TMemoryStream.Create; try stream.LoadFromFile(filename); result := EncodeBase64(stream.Memory, stream.Size); finally FreeAndNil(stream); end; end; procedure TFDMFE.InviaFE(Autorizzazione, PercorsoFileXML, Credenziali, Dominio : string); var lHTTP: TIdHTTP; lParamList: TStringList; Comando, RisultatoPost : string; begin //************ COMANDO HTTP ******** Comando := 'https://ws.fatturazioneelettronica.aruba.it/services/invoice/upload'; //********************************** lParamList := TStringList.Create; Credenziali := '""'; Dominio := '""'; try lParamList.Add('"dataFile":"'+EncodeFile(PercorsoFileXML)+'"'); lParamList.Add('"credential":'+Credenziali); // vuoti lParamList.Add('"domain":'+Dominio); // vuoti 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.Add('Authorization:Bearer ' + Autorizzazione); lHTTP.Request.ContentType := 'application/json'; lHTTP.Request.CharSet := 'UTF-8'; try RisultatoPost := lHTTP.Post(Comando, lParamList); except on E: EIdHTTPProtocolException do begin Memo1.Lines.Add(E.ErrorMessage); end; end; finally lHTTP.Free; end; finally lParamList.Free; end; end; I have this error: {"timestamp":"2019-03-01T21:45:29.634+0000","status":400,"error":"Bad Request","message":"JSON parse error: Unexpected character ('%' (code 37)): expected a valid value (number, String, array, object, 'true', 'false' or 'null'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('%' (code 37)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: (PushbackInputStream); line: 1, column: 2]","path":"/core-trasmissione-rest-fattura/services/invoice/upload"} THIS IS THE DOCUMENTATION: 7.1. Upload invoice Code: POST /services/invoice/upload Questo metodo deve essere utilizzato dall’OE per effettuare l’invio di una fattura già nel formato XML secondo norme AGID, ma non ancora firmata digitalmente. HTTP request Code: POST /services/invoice/upload HTTP/1.1 I parametri [i]"domain" e "credential" rappresentano rispettivamente il dominio e le credenziali di firma automatica se possedute dall’utente, in caso contrario lasciare tali campi vuoti.[/i] Request headers Name Description Code: Authorization Provisioner access token Request fields Path Type Description Code: dataFile Code: String Dati allegato in formato encoded Base64 Code: credential Code: String Credenziali firma Code: domain Code: String Domain firma HTTP response Code: HTTP/1.1 200 OK Response fields Path Type Description Code: uploadFileName Code: String Nome file fattura (inviato a SdI) restituito dal ws Code: errorCode Code: String Eventuale codice di errore Code: errorDescription Code: String Eventuale descrizione errore thanks RE: problem with post methos https - URGENT - rlebeau - 03-01-2019 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: Code: procedure TFDMFE.InviaFE(Autorizzazione, PercorsoFileXML, Credenziali, Dominio : string); Alternatively: Code: uses RE: problem with post methos https - URGENT - staff@ergosoft.it - 03-02-2019 Hi, thanks now is ok.... I have use this... with TJSONObject.Create do try AddPair('dataFile', EncodeFile(PercorsoFileXML)); AddPair('credential', Credenziali); AddPair('domain', Dominio); lPostData.Writestring(ToJSON); // this line is changed********************** finally Free; end; |