Recent

Author Topic: [SOLVED] Retrieve hex unicode from TIdHTTP or tfphttpclient response  (Read 4787 times)

wisheskernel

  • New member
  • *
  • Posts: 7
Whether I use TIdHTTP or  tfphttpclient  post method to request specific URL, the non-English character in the response always display hex unicode like
Quote
\u8a8d\u8b49
.  Do I miss doing something? or How can I convert these to normal unicode string?  Any help is going to be appreciated.

My environment: Windows7 64bit, FPC 3.0.4, Lazarus 2.0.0  x86_64 , indylaz 10.6.2

The code with Indy:
Code: Pascal  [Select][+][-]
  1.     // ...
  2.     RequestBody := TStringStream.Create(ReqBody);
  3.  
  4.     IdHTTP1.Request.ContentType := 'application/json';
  5.     IdHTTP1.Request.CharSet := 'utf-8';
  6.  
  7.     Try
  8.        ResponseBody := IdHTTP1.Post(url, RequestBody, IndyTextEncoding_UTF8);
  9.        Memo1.Lines.Add(ResponseBody);
  10.     finally
  11.        RequestBody.Free;
  12.     end;                                        
  13.  

The code with  tfphttpclient :
Code: Pascal  [Select][+][-]
  1.   //...
  2.   http:= tfphttpclient.Create(nil);
  3.   RequestBody := TStringStream.Create(strbody);
  4.  
  5.   try
  6.     http.AllowRedirect := true;
  7.     http.AddHeader('User-Agent','Mozilla 5.0 (compatible ct)');
  8.     http.AddHeader('Content-Type','application/json;charset=UTF-8');
  9.     http.RequestBody := RequestBody;
  10.     Memo1.Lines.Add(http.Post(url));
  11.   finally
  12.      http.Free;
  13.      RequestBody.Free;
  14.   end;                                        
  15.  
« Last Edit: October 12, 2020, 08:58:15 am by wisheskernel »

PascalDragon

  • Hero Member
  • *****
  • Posts: 6421
  • Compiler Developer
Re: Retrieve hex unicode from TIdHTTP or tfphttpclient response
« Reply #1 on: October 08, 2020, 01:28:02 pm »
Whether I use TIdHTTP or  tfphttpclient  post method to request specific URL, the non-English character in the response always display hex unicode like
Quote
\u8a8d\u8b49
.  Do I miss doing something? or How can I convert these to normal unicode string?  Any help is going to be appreciated.

That is simply how JSON is encoded. Use some JSON library (like fpJson) to load the response and work on that. Those will deal with the escape sequences.

wisheskernel

  • New member
  • *
  • Posts: 7
Re: Retrieve hex unicode from TIdHTTP or tfphttpclient response
« Reply #2 on: October 12, 2020, 08:57:03 am »
Deep gratitude towards PascalDragon. The function JSONStringtoString convert these hex codes to normal words.

 

TinyPortal © 2005-2018