Error in TRESTClient file download: No mapping for unicode character exists in target multi-byte code page

110 Views Asked by At

I want to download files with the TRESTClient library. However on some files I get the following error on the line of RESTRequest1.Execute:

Project dllhst3g.exe raised exception class ERESTException with message 'REST request failed: Keine Zuordnung für Unicode-Zeichen in der Multibyte-Zielcodeseite vorhanden'.

English:

No mapping for unicode character exists in target multi-byte code page

According to Fiddler, the request is sent and gets a response, which is a perfectly readable text file(Content-Type: text/plain). This looks like an encoding issue during the creation of the TRESTResponse object. The Content-Disposition response header is UTF-8 encoded, maybe that's the reason?:

Content-Disposition: attachment; filename*=utf-8''my%20%28filename%29.TXT

My code looks like this:

var
  RESTClient1: TRESTClient;
  RESTRequest1: TRESTRequest;

RESTClient1 := TRESTClient.Create(myUrl);
RESTRequest1 := TRESTRequest.Create(RESTClient1);
RESTRequest1.AddAuthParameter('Authorization', 'Bearer ' + mySessionId, pkHTTPHEADER, [poDoNotEncode]);
RESTRequest1.AddParameter('Accept', 'application/octet-stream', pkHTTPHEADER, [poDoNotEncode]);
RESTRequest1.Execute;

if RESTRequest1.Response.StatusCode <> 200 then
begin
  // raise Exception
end;

result := RESTRequest1.Response.RawBytes;

How can I make sure that the response uses the correct encoding?

1

There are 1 best solutions below

0
JonasK On

Since I couldn´t find a solution with the TRESTClient library, I´ll use Indy for now as a workaround:

using
  IdHTTP, IdSSLOpenSSL, IdGlobal;

var
  lHTTP: TIdHTTP;

lHTTP := TIdHTTP.Create;
try
  lHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
  lHTTP.IOHandler.MaxLineAction := maException;
  (lHTTP.IOHandler as TIdSSLIOHandlerSocketOpenSSL).SSLOptions.Method := sslvSSLv23;

  result := TStringStream.Create;
  lHTTP.Request.CustomHeaders.AddValue('Authorization', 'Bearer ' + mySessionId);
  lHTTP.Request.CustomHeaders.AddValue('Accept', 'application/octet-stream');

  try
    lHTTP.Get(myUrl, result);
  except on e: EIdHTTPProtocolException do
    raise Exception.Create(e.Message + ': ' + e.ErrorMessage);
  end;
finally
  lHTTP.Free;
end;