Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

RESTRequest No Mapping for unicode Char - AIUTO

106 views
Skip to first unread message

Davide A

unread,
Oct 3, 2023, 5:42:34 AM10/3/23
to
Salve a tutti
E' un bel po che cerco di risolvere questo affannoso problema:
COnnessione ad un server REST, con autenticazione OAuth2, scambio di richieste funziona correttamente, ma quando vado a fare l'ultima richiesta, nel quale dovrei trovare il pdf nel Raw ( che in effetti esiste nel response ) la chiamata di RestRequest.execute, genera l'errore "No Mapping for unicode Character exists in te target multi-bytre code page"
analizzando la unit REST.Client, il problema sembra esser alla riga 2948 : if LCharSet <> '' then LContentIsString := True

per cui poche righe più in basso :
if LContentIsString then
LContent := FClient.HTTPClient.Response.ContentAsString(LEncoding);

per la quale si genera l'errore,



Può qualcuno indicarmi una soluzione a cui non riesco ad arrivare?
ho provato anche a fare una chiamata diretta http ( anche Indy ) ma ci sono difficoltà a passare le autorizzazione e i token, oppure l'utilizzo di SSL

Grazie

paolocarfi

unread,
Oct 3, 2023, 10:43:15 AM10/3/23
to
A spanne pare che, il problema dovrebbe essere nella risposta del server.
Per riuscire a capire necessita il codice del server che crea la risposta e i dati della risposta completi, incluse le intestazioni HTTP.

Davide A

unread,
Oct 3, 2023, 1:27:30 PM10/3/23
to
Grazie per l'interessamento ma, il server non è di mia competenza, ed ho scritto loro proprio perchè sembra che risponda come Charset=UTF8, ma loro hanno risposto che non settano nulla, anzi il content-Type come application/pdf, ma l'if (if LCharSet <> '' then LContentIsString := True ) della unit REST.Client, riporta a TRUE ...infatti è settato come UTF-8

paolocarfi

unread,
Oct 4, 2023, 1:39:08 AM10/4/23
to
prova a cambiare nella request

RESTRequest1.Client.FallbackCharsetEncoding := 'raw';

potrebbe funzionare

Davide A

unread,
Oct 4, 2023, 4:42:20 AM10/4/23
to
Grazie, Purtroppo NO
Client.HTTPClient.Response.CharSet (nella REST.Client unit) ritorna comunque UTF-8

ma come posso recompilare la unit ( REST.Client) ? così forzo Delphi?

paolocarfi

unread,
Oct 4, 2023, 11:47:48 AM10/4/23
to
Il messagio chi ricevi viene prodotto da errato encoding, questo è certo.
Il vero problema è capire cosa fai e cosa ti aspetti che avvenga.
Nessuno a detto che devi ricompilare la unit REST.Client. Ti invitavo ad aggiungere: (Il tuo componente di request).Client.FallbackCharsetEncoding := 'raw';

Alberto Salvati

unread,
Oct 5, 2023, 2:28:41 AM10/5/23
to
Prima di tutto, modificare e ricompilare una unit nativa di delphi in genere non è una buona idea....salvo che non vi sia un BUG ACCERTATO.


Ipotizzando la congruità dei messaggi di errore, è come se il server mandasse dati in un formato (utf8, unicode, mbcs...) ma il client non fosse allineato allo stesso formato.

Per la cronaca, ho scontri praticamente ogni giorno per situazioni analoghe, ma relative a conversioni ANSI/UNICODE e viceversa, che però non "scoppiano" ma producono dati sporchi.


Intanto, potresti provare ad un invocare il REST da un client che NON sia la tua applicazione.
Ci sono vari strumenti (applicazioni e/o siti web) che ti permettono di fare queste prove.
Mo su 2 piedi putroppo non so darti suggerimenti...è un pò che non uso REST e, sopratutto, non l'ho mai usato in delphi....(PUAH!).
..la butto li..magari chi espone il servizio ha una interfaccia web per fare delle prove?
In questo modo riusciresti a capire se il problema è TUO, LEGATO A DELPHI CHE ORARAMI FUNZIONA IN MODO CINOFALLICO, oppure magari hai beccato un caso che il servizio REST non gestisce....


Altra cosa, potresti postare un po di codice giusto per aiutare a contestualizzare?

Infine, ho trovato un post che descrive un caso molto simile se non addirittura uguale al tuo...

https://www.google.com/search?q=delphi+rest+No+Mapping+for+unicode+Character+exists+in+the+target+multi-bytre+code+page&client=firefox-b-d&sca_esv=570874343&ei=gFUeZdPvN-iIi-gP6IOGwAg&ved=0ahUKEwjTr7nhod6BAxVoxAIHHeiBAYgQ4dUDCA8&uact=5&oq=delphi+rest+No+Mapping+for+unicode+Character+exists+in+the+target+multi-bytre+code+page&gs_lp=Egxnd3Mtd2l6LXNlcnAiV2RlbHBoaSByZXN0IE5vIE1hcHBpbmcgZm9yIHVuaWNvZGUgQ2hhcmFjdGVyIGV4aXN0cyBpbiB0aGUgdGFyZ2V0IG11bHRpLWJ5dHJlIGNvZGUgcGFnZTIKEAAYRxjWBBiwAzIKEAAYRxjWBBiwAzIKEAAYRxjWBBiwAzIKEAAYRxjWBBiwAzIKEAAYRxjWBBiwA0j2IFDMCFi1HnABeAGQAQCYAQCgAQCqAQC4AQPIAQD4AQHiAwQYACBBiAYBkAYF&sclient=gws-wiz-serp&bshm=rime/1

A.

Alberto Salvati

unread,
Oct 5, 2023, 2:43:12 AM10/5/23
to
Altro post...non c'è una risposta ma magari da un cfr con il tuo codice puoi rtovare qualcosa...

https://www.tek-tips.com/viewthread.cfm?qid=1811003

Davide A

unread,
Oct 5, 2023, 4:52:17 AM10/5/23
to
Si grazie, infatti il "PURTROPPO NO" si riferiva al fatto che avevo provato e NON ha cambiato l'esito.

Non condivido il fatto di "patchare" una UNIT nativa, ma sono fermo con questa problematica da oltre 2mesi e devo consegnare il prodotto

Davide A

unread,
Oct 5, 2023, 5:05:56 AM10/5/23
to
il codice .....
// Prima connessione richiesta TOKEN
RESTRequest1.Params.Clear;

RESTRequest1.AddParameter('grant_type', 'client_credentials', pkGETorPOST);
RESTRequest1.AddParameter('consumer_key','xxxxxxxxxxx', pkGETorPOST);
RESTRequest1.AddParameter('consumer_secret','xxxxx', pkGETorPOST);

RESTClient1.BaseURL := 'https://api.xxxxxx.com/';

// Richiesta
RESTRequest1.Method := rmPOST;
RESTRequest1.Resource := '/auth/token';
RESTRequest1.Execute;


// Estrai
jValue := RESTResponse1.JSONValue;
edToken.Text := jValue.GetValue<string>('access_token');

// Imposta TOKEN su REST Client e OAuth2
OAuth2Authenticator1.AccessTokenParamName := 'access_token';
OAuth2Authenticator1.AccessToken := edToken.Text;
OAuth2Authenticator1.TokenType := TOAuth2TokenType.ttBEARER;
RESTClient1.Authenticator := OAuth2Authenticator1;



// Richiedo pdf
RESTRequest1.Params.Clear;
OAuth2Authenticator1.AccessTokenParamName := 'attachment_token';
// 'access_token';
OAuth2Authenticator1.AccessToken := edTmpToken.Text;

RESTRequest1.Method := rmGET;
RESTRequest1.Resource := '/attachments/' + edTmpId.Text; // id Entity

RESTClient1.ContentType := 'ctAPPLICATION_PDF';
RESTRequest1.Client.FallbackCharsetEncoding := 'raw';

RESTRequest1.Accept := 'application/pdf';
\\RESTRequest1.AcceptCharset := 'UTF-8';

try
RESTRequest1.Execute;
finally
nSize := length( RESTResponse1.RawBytes ); // nSize := length(RESTRequest1.Response.RawBytes);
if nSize > 0 then begin
TFile.WriteAllBytes( EarLogFileName('pdf'), RESTResponse1.RawBytes);

meLog.Lines.Append(EarBytesToStr(nSize) + ' download');
end
else meLog.Lines.Append('Zero Bytes download');
end;






La risposta dell'azienda del server:
abbiamo verificato la chiamata per lo scarico del PDF, a noi non risulta che venga indicato il Charset=UTF-8, è possibile che venga inserito dal vostro applicativo?
In ogni caso, il file che viene inviato è un PDF in binario , qui di seguito potete trovare l’header della risposta di una richiesta di scarico PDF:

...riporto a mano le info di interesse..:
Content-Type : application/pdf
Content-Length : 101556
Content-Disposition : attachment;filename="asas.pdf"
Content-Encoding : gzip
Vary : Origin



paolocarfi

unread,
Oct 5, 2023, 7:05:24 AM10/5/23
to
dovresti aggiungere questo
RESTResponse1.ContentEncoding := 'gzip';
e lanciare una funzione che decodifica il response
guarda
https://www.fmxexpress.com/decode-gzip-content-from-the-rest-client-in-delphi-xe7-firemonkey-on-android-and-ios/

Davide A

unread,
Oct 5, 2023, 9:03:08 AM10/5/23
to
ho aggiunto
RESTResponse1.ContentEncoding := 'gzip';
RESTResponse1.ContentType := 'ctAPPLICATION_PDF';


ma da sempre lo stesso errore!
il problema è che se forzo ( tramite il debug nel REST.Client : LContentIsString = FALSE ) Tutto funziona!! e RESTResponse1.RawBytes mi ritorna il pdf...sto impazzendo

paolocarfi

unread,
Oct 7, 2023, 2:17:37 AM10/7/23
to
prova cosi
RESTResponse1.ContentType := '';

Alberto Salvati

unread,
Oct 9, 2023, 6:10:37 AM10/9/23
to
Giusto per scrupolo, prva cosi e vedi cosa succede....


VAR
INFO: STRING;

....

try
TRY
INFO := 'BEFORE EXECUTE';
RESTRequest1.Execute;
INFO := 'BEFORE SIZE';
nSize := length( RESTResponse1.RawBytes ); // nSize := length(RESTRequest1.Response.RawBytes);
> if nSize > 0 then begin
INFO := 'BEFORE WRITE FILE';
> TFile.WriteAllBytes( EarLogFileName('pdf'), RESTResponse1.RawBytes);
INFO := 'AFTER WRITE FILE';
>
> meLog.Lines.Append(EarBytesToStr(nSize) + ' download');
> end
> else meLog.Lines.Append('Zero Bytes download');
> end;

EXCEPT
ON E:EXCEPTION DO BEGIN
SHOWMESSAGE('EXCEPTION ' + E.CLASSNAME + ' ' + E.MESSAGE + ' INFO:' + INFO;

END

finally
END;

Davide A

unread,
Nov 2, 2023, 5:31:53 AM11/2/23
to
Grazie ma il problema è sulla Execute, ho dovuto patchare la unit per forzare la lettura 'raw' se si tratta di pdf



Adesso la stessa unit da un altro errore al termine dell'applicazione, se , e solo se, è stato eseguito un RESTRequest.execute : nel destructor TCustomRESTResponse.Destroy; la riga
FreeAndNil(FJSONValue);
genera un A.V.

a nulla sono valsi i reset del valore JSONValue, che anche tramite la sua procedura interna ResetToDefaults, genera cmq A.V.
ho aperto altro post
https://groups.google.com/g/it.comp.lang.delphi/c/Gtiy5Hg_KOk
0 new messages